home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / comms / non-internet / samba / source / util.c < prev    next >
C/C++ Source or Header  |  1996-06-26  |  108KB  |  4,454 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    Samba utility functions
  5.    Copyright (C) Andrew Tridgell 1992-1995
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "includes.h"
  23. #include "loadparm.h"
  24.  
  25. pstring scope = "";
  26.  
  27. int DEBUGLEVEL = 1;
  28.  
  29. BOOL passive = False;
  30.  
  31. int Protocol = PROTOCOL_COREPLUS;
  32.  
  33. int serverzone=0;
  34.  
  35. /* a default finfo structure to ensure all fields are sensible */
  36. file_info def_finfo = {-1,0,0,0,0,0,0,""};
  37.  
  38. /* these are some file handles where debug info will be stored */
  39. FILE *dbf = NULL;
  40.  
  41. /* the client file descriptor */
  42. int Client = -1;
  43.  
  44. /* info on the client */
  45. struct from_host Client_info=
  46. {"UNKNOWN","0.0.0.0",NULL};
  47.  
  48. /* the last IP received from */
  49. struct in_addr lastip;
  50.  
  51. /* the last port received from */
  52. int lastport=0;
  53.  
  54. /* my IP, the broadcast IP and the Netmask */
  55. struct in_addr myip;
  56. struct in_addr bcast_ip;
  57. struct in_addr Netmask;
  58.  
  59. int trans_num = 0;
  60.  
  61. /*
  62.    case handling on filenames 
  63. */
  64. int case_default = CASE_LOWER;
  65.  
  66.  
  67. /* size of reads during a direct file to file transfer */
  68. int ReadSize = 16*1024;
  69.  
  70. pstring debugf = "/tmp/log.samba";
  71. int syslog_level;
  72.  
  73. /* the following control case operations - they are put here so the
  74.    client can link easily */
  75. BOOL case_sensitive;
  76. BOOL case_preserve;
  77. BOOL use_mangled_map = False;
  78. BOOL short_case_preserve;
  79. BOOL case_mangle;
  80.  
  81. fstring remote_machine="";
  82. fstring local_machine="";
  83. fstring remote_arch="UNKNOWN";
  84. fstring remote_proto="UNKNOWN";
  85. pstring myhostname="";
  86. pstring user_socket_options="";   
  87. pstring sesssetup_user="";
  88.  
  89.  
  90. static char *filename_dos(char *path,char *buf);
  91.  
  92. static BOOL stdout_logging = False;
  93.  
  94.  
  95. /*******************************************************************
  96.   get ready for syslog stuff
  97.   ******************************************************************/
  98. void setup_logging(char *pname,BOOL interactive)
  99. {
  100. #ifdef SYSLOG
  101.   if (!interactive) {
  102.     char *p = strrchr(pname,'/');
  103.     if (p) pname = p+1;
  104.     openlog(pname, LOG_PID, LOG_DAEMON);
  105.   }
  106. #endif
  107.   if (interactive) {
  108.     stdout_logging = True;
  109.     dbf = stdout;
  110.   }
  111. }
  112.  
  113.  
  114. BOOL append_log=False;
  115.  
  116.  
  117. /****************************************************************************
  118. reopen the log files
  119. ****************************************************************************/
  120. void reopen_logs(void)
  121. {
  122.   extern FILE *dbf;
  123.   pstring fname;
  124.   
  125.   if (DEBUGLEVEL > 0)
  126.     {
  127.       strcpy(fname,debugf);
  128.       if (lp_loaded() && (*lp_logfile()))
  129.     strcpy(fname,lp_logfile());
  130.  
  131.       if (!strcsequal(fname,debugf) || !dbf || !file_exist(debugf,NULL))
  132.     {
  133.       strcpy(debugf,fname);
  134.       if (dbf) fclose(dbf);
  135.       if (append_log)
  136.         dbf = fopen(debugf,"a");
  137.       else
  138.         dbf = fopen(debugf,"w");
  139.       if (dbf) setbuf(dbf,NULL);
  140.     }
  141.     }
  142.   else
  143.     {
  144.       if (dbf)
  145.     {
  146.       fclose(dbf);
  147.       dbf = NULL;
  148.     }
  149.     }
  150. }
  151.  
  152.  
  153. /*******************************************************************
  154. write an debug message on the debugfile. This is called by the DEBUG
  155. macro
  156. ********************************************************************/
  157. #ifdef __STDC__
  158. int Debug1(char *format_str, ...)
  159. {
  160. #else
  161. int Debug1(va_alist)
  162. va_dcl
  163. {  
  164.   char *format_str;
  165. #endif
  166.   va_list ap;  
  167.   
  168. #ifdef __STDC__
  169.   va_start(ap, format_str);
  170. #else
  171.   va_start(ap);
  172.   format_str = va_arg(ap,char *);
  173. #endif
  174.  
  175.   if (stdout_logging) {
  176.     vfprintf(dbf,format_str,ap);
  177.     va_end(ap);
  178.     return(0);
  179.   }
  180.  
  181.   {
  182.     static int debug_count=0;
  183.  
  184.     debug_count++;
  185.     if (debug_count == 100) {
  186.       int maxlog = lp_max_log_size() * 1024;
  187.       if (dbf && maxlog > 0)
  188.     {
  189.       struct stat st;
  190.  
  191.       if (fstat(fileno(dbf),&st) == 0 && st.st_size > maxlog) {
  192.         fclose(dbf); dbf = NULL;
  193.         reopen_logs();
  194.         if (dbf && file_size(debugf) > maxlog) {
  195.           pstring name;
  196.           fclose(dbf); dbf = NULL;
  197.           sprintf(name,"%s.old",debugf);
  198.           sys_rename(debugf,name);
  199.           reopen_logs();
  200.         }
  201.       }
  202.     }
  203.       debug_count=0;
  204.     }
  205.   }
  206.   
  207. #ifdef SYSLOG
  208.   if (!lp_syslog_only())
  209. #endif  
  210.     {
  211.       if (!dbf) 
  212.     {
  213.             dbf = fopen(debugf,"w");
  214.       if (dbf)
  215.         setbuf(dbf,NULL);
  216.       else
  217.         return(0);
  218.     }
  219.     }
  220.  
  221. #ifdef SYSLOG
  222.   if (syslog_level < lp_syslog())
  223.     {
  224.       /* 
  225.        * map debug levels to syslog() priorities
  226.        * note that not all DEBUG(0, ...) calls are
  227.        * necessarily errors
  228.        */
  229.       static int priority_map[] = { 
  230.     LOG_ERR,     /* 0 */
  231.     LOG_WARNING, /* 1 */
  232.     LOG_NOTICE,  /* 2 */
  233.     LOG_INFO,    /* 3 */
  234.       };
  235.       int priority;
  236.       pstring msgbuf;
  237.       
  238.       if (syslog_level >= sizeof(priority_map) / sizeof(priority_map[0]) ||
  239.       syslog_level < 0)
  240.     priority = LOG_DEBUG;
  241.       else
  242.     priority = priority_map[syslog_level];
  243.       
  244.       vsprintf(msgbuf, format_str, ap);
  245.       
  246.       msgbuf[255] = '\0';
  247.       syslog(priority, "%s", msgbuf);
  248.     }
  249. #endif
  250.   
  251. #ifdef SYSLOG
  252.   if (!lp_syslog_only())
  253. #endif
  254.     {
  255.       vfprintf(dbf,format_str,ap);
  256.       fflush(dbf);
  257.     }
  258.   
  259.   va_end(ap);
  260.   return(0);
  261. }
  262.  
  263. /****************************************************************************
  264. routine to do file locking
  265. ****************************************************************************/
  266. BOOL fcntl_lock(int fd,int op,uint32 offset,uint32 count,int type)
  267. {
  268. #if HAVE_FCNTL_LOCK
  269.   struct flock lock;
  270.   int ret;
  271.  
  272. #if 1
  273.   uint32 mask = 0xC0000000;
  274.  
  275.   /* make sure the count is reasonable, we might kill the lockd otherwise */
  276.   count &= ~mask;
  277.  
  278.   /* the offset is often strange - remove 2 of its bits if either of
  279.      the top two bits are set. Shift the top ones by two bits. This
  280.      still allows OLE2 apps to operate, but should stop lockd from
  281.      dieing */
  282.   if ((offset & mask) != 0)
  283.     offset = (offset & ~mask) | ((offset & mask) >> 2);
  284. #else
  285.   unsigned long mask = ((unsigned)1<<31);
  286.  
  287.   /* interpret negative counts as large numbers */
  288.   if (count < 0)
  289.     count &= ~mask;
  290.  
  291.   /* no negative offsets */
  292.   offset &= ~mask;
  293.  
  294.   /* count + offset must be in range */
  295.   while ((offset < 0 || (offset + count < 0)) && mask)
  296.     {
  297.       offset &= ~mask;
  298.       mask = mask >> 1;
  299.     }
  300. #endif
  301.  
  302.  
  303.   DEBUG(5,("fcntl_lock %d %d %d %d %d\n",fd,op,(int)offset,(int)count,type));
  304.  
  305.   lock.l_type = type;
  306.   lock.l_whence = SEEK_SET;
  307.   lock.l_start = (int)offset;
  308.   lock.l_len = (int)count;
  309.   lock.l_pid = 0;
  310.  
  311.   errno = 0;
  312.  
  313.   ret = fcntl(fd,op,&lock);
  314.  
  315.   if (errno != 0)
  316.     DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
  317.  
  318.   /* a lock query */
  319.   if (op == F_GETLK)
  320.     {
  321.       if ((ret != -1) &&
  322.       (lock.l_type != F_UNLCK) && 
  323.       (lock.l_pid != 0) && 
  324.       (lock.l_pid != getpid()))
  325.     {
  326.       DEBUG(3,("fd %d is locked by pid %d\n",fd,lock.l_pid));
  327.       return(True);
  328.     }
  329.  
  330.       /* it must be not locked or locked by me */
  331.       return(False);
  332.     }
  333.  
  334.   /* a lock set or unset */
  335.   if (ret == -1)
  336.     {
  337.       DEBUG(3,("lock failed at offset %d count %d op %d type %d (%s)\n",
  338.            offset,count,op,type,strerror(errno)));
  339.  
  340.       /* perhaps it doesn't support this sort of locking?? */
  341.       if (errno == EINVAL)
  342.     {
  343.       DEBUG(3,("locking not supported? returning True\n"));
  344.       return(True);
  345.     }
  346.  
  347.       return(False);
  348.     }
  349.  
  350.   /* everything went OK */
  351.   DEBUG(5,("Lock call successful\n"));
  352.  
  353.   return(True);
  354. #else
  355.   return(False);
  356. #endif
  357. }
  358.  
  359. /*******************************************************************
  360. lock a file - returning a open file descriptor or -1 on failure
  361. The timeout is in seconds. 0 means no timeout
  362. ********************************************************************/
  363. int file_lock(char *name,int timeout)
  364. {  
  365.   int fd = open(name,O_RDWR|O_CREAT,0666);
  366.   time_t t=0;
  367.   if (fd < 0) return(-1);
  368.  
  369. #if HAVE_FCNTL_LOCK
  370.   if (timeout) t = time(NULL);
  371.   while (!timeout || (time(NULL)-t < timeout)) {
  372.     if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)) return(fd);    
  373.     msleep(LOCK_RETRY_TIMEOUT);
  374.   }
  375.   return(-1);
  376. #else
  377.   return(fd);
  378. #endif
  379. }
  380.  
  381. /*******************************************************************
  382. unlock a file locked by file_lock
  383. ********************************************************************/
  384. void file_unlock(int fd)
  385. {
  386.   if (fd<0) return;
  387. #if HAVE_FCNTL_LOCK
  388.   fcntl_lock(fd,F_SETLK,0,1,F_UNLCK);
  389. #endif
  390.   close(fd);
  391. }
  392.  
  393. /*******************************************************************
  394. a gettimeofday wrapper
  395. ********************************************************************/
  396. void GetTimeOfDay(struct timeval *tval)
  397. {
  398. #ifdef GETTIMEOFDAY1
  399.   gettimeofday(tval);
  400. #else
  401.   gettimeofday(tval,NULL);
  402. #endif
  403. }
  404.  
  405. int extra_time_offset = 0;
  406.  
  407. static int timediff = 0;
  408.  
  409. /*******************************************************************
  410. init the time differences
  411. ********************************************************************/
  412. void TimeInit(void)
  413. {
  414.   struct tm tm_utc,tm_local;
  415.   time_t t;
  416.  
  417.   t = time(NULL);
  418.  
  419.   tm_utc = *(gmtime(&t));
  420.   tm_local = *(localtime(&t));
  421.  
  422. #ifdef HAVE_GMTOFF
  423.   timediff = -tm_local.tm_gmtoff;  
  424. #else
  425.   timediff = mktime(&tm_utc) - mktime(&tm_local);
  426. #endif
  427.  
  428.   if (serverzone == 0) {
  429.     serverzone = timediff - DSTDiff(t);
  430.     DEBUG(4,("Serverzone is %d\n",serverzone));
  431.   }
  432. }
  433.  
  434.  
  435. /*******************************************************************
  436. return the DST offset for a particular time
  437. We keep a table of DST offsets to prevent calling localtime() on each 
  438. call of this function. This saves a LOT of time on many unixes.
  439. ********************************************************************/
  440. int DSTDiff(time_t t)
  441. {
  442.   static struct dst_table {time_t start,end; BOOL is_dst;} *dst_table = NULL;
  443.   static int table_size = 0;
  444.   int i;
  445.   BOOL is_dst = False;
  446.  
  447.   if (t == 0) t = time(NULL);
  448.  
  449. #ifndef NO_ISDST
  450.   for (i=0;i<table_size;i++)
  451.     if (t >= dst_table[i].start && t <= dst_table[i].end) break;
  452.  
  453.   if (i<table_size) {
  454.     is_dst = dst_table[i].is_dst;
  455.   } else {
  456.     time_t low,high;
  457.  
  458.     dst_table = (struct dst_table *)Realloc(dst_table,
  459.                           sizeof(dst_table[0])*(i+1));
  460.     if (!dst_table) {
  461.       table_size = 0;
  462.       return(0);
  463.     }
  464.  
  465.     table_size++;
  466.  
  467.     dst_table[i].is_dst = is_dst = (localtime(&t)->tm_isdst?True:False);;
  468.     dst_table[i].start = dst_table[i].end = t;
  469.     
  470.     /* no entry will cover more than 6 months */
  471.     low = t - 3*30*24*60*60;
  472.     high = t + 3*30*24*60*60;
  473.  
  474.     /* widen the new entry using two bisection searches */
  475.     while (low+60*60 < dst_table[i].start) {
  476.       t = low + (dst_table[i].start-low)/2;
  477.       if ((localtime(&t)->tm_isdst?True:False) == is_dst)
  478.     dst_table[i].start = t;
  479.       else
  480.     low = t;
  481.     }
  482.  
  483.     while (high-60*60 > dst_table[i].end) {
  484.       t = high + (high-dst_table[i].end)/2;
  485.       if ((localtime(&t)->tm_isdst?True:False) == is_dst)
  486.     dst_table[i].end = t;
  487.       else
  488.     high = t;
  489.     }    
  490.  
  491. /*
  492.     DEBUG(1,("Added DST entry from %s ",
  493.          asctime(localtime(&dst_table[i].start))));
  494.     DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)),
  495.          dst_table[i].is_dst));
  496. */
  497.   }
  498. #endif
  499.  
  500.   return((is_dst?60*60:0) - (extra_time_offset*60));
  501. }
  502.  
  503. /****************************************************************************
  504. return the difference between local and GMT time
  505. ****************************************************************************/
  506. int TimeDiff(time_t t)
  507. {
  508.   static BOOL initialised = False;
  509.   if (!initialised) {initialised=True; TimeInit();}
  510.   return(timediff - DSTDiff(t));
  511. }
  512.  
  513. /****************************************************************************
  514. try to optimise the localtime call, it can be quite expenive on some machines
  515. timemul is normally LOCAL_TO_GMT, GMT_TO_LOCAL or 0
  516. ****************************************************************************/
  517. struct tm *LocalTime(time_t *t,int timemul)
  518. {
  519.   time_t t2 = *t;
  520.  
  521.   if (timemul)
  522.     t2 += timemul * TimeDiff(t2);
  523.  
  524.   return(gmtime(&t2));
  525. }
  526.  
  527.  
  528. /****************************************************************************
  529. determine if a file descriptor is in fact a socket
  530. ****************************************************************************/
  531. BOOL is_a_socket(int fd)
  532. {
  533.   int v,l;
  534.   l = sizeof(int);
  535.   return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
  536. }
  537.  
  538.  
  539. /****************************************************************************
  540.   Get the next token from a string, return False if none found
  541.   handles double-quotes. 
  542. Based on a routine by GJC@VILLAGE.COM. 
  543. Extensively modified by Andrew.Tridgell@anu.edu.au
  544. ****************************************************************************/
  545. BOOL next_token(char **ptr,char *buff,char *sep)
  546. {
  547.   static char *last_ptr=NULL;
  548.   char *s;
  549.   BOOL quoted;
  550.  
  551.   if (!ptr) ptr = &last_ptr;
  552.   if (!ptr) return(False);
  553.  
  554.   s = *ptr;
  555.  
  556.   /* default to simple separators */
  557.   if (!sep) sep = " \t\n\r";
  558.  
  559.   /* find the first non sep char */
  560.   while(*s && strchr(sep,*s)) s++;
  561.  
  562.   /* nothing left? */
  563.   if (! *s) return(False);
  564.  
  565.   /* copy over the token */
  566.   for (quoted = False; *s && (quoted || !strchr(sep,*s)); s++)
  567.     {
  568.       if (*s == '\"') 
  569.     quoted = !quoted;
  570.       else
  571.     *buff++ = *s;
  572.     }
  573.  
  574.   *ptr = (*s) ? s+1 : s;  
  575.   *buff = 0;
  576.   last_ptr = *ptr;
  577.  
  578.   return(True);
  579. }
  580.  
  581. /*******************************************************************
  582. safely copies memory, ensuring no overlap problems.
  583. this is only used if the machine does not have it's own memmove().
  584. this is not the fastest algorithm in town, but it will do for our
  585. needs.
  586. ********************************************************************/
  587. void *MemMove(void *dest,void *src,int size)
  588. {
  589.   unsigned long d,s;
  590.   int i;
  591.   if (dest==src || !size) return(dest);
  592.  
  593.   d = (unsigned long)dest;
  594.   s = (unsigned long)src;
  595.  
  596.   if ((d >= (s+size)) || (s >= (d+size))) {
  597.     /* no overlap */
  598.     memcpy(dest,src,size);
  599.     return(dest);
  600.   }
  601.  
  602.   if (d < s)
  603.     {
  604.       /* we can forward copy */
  605.       if (s-d >= sizeof(int) && 
  606.       !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
  607.     /* do it all as words */
  608.     int *idest = (int *)dest;
  609.     int *isrc = (int *)src;
  610.     size /= sizeof(int);
  611.     for (i=0;i<size;i++) idest[i] = isrc[i];
  612.       } else {
  613.     /* simplest */
  614.     char *cdest = (char *)dest;
  615.     char *csrc = (char *)src;
  616.     for (i=0;i<size;i++) cdest[i] = csrc[i];
  617.       }
  618.     }
  619.   else
  620.     {
  621.       /* must backward copy */
  622.       if (d-s >= sizeof(int) && 
  623.       !(s%sizeof(int)) && !(d%sizeof(int)) && !(size%sizeof(int))) {
  624.     /* do it all as words */
  625.     int *idest = (int *)dest;
  626.     int *isrc = (int *)src;
  627.     size /= sizeof(int);
  628.     for (i=size-1;i>=0;i--) idest[i] = isrc[i];
  629.       } else {
  630.     /* simplest */
  631.     char *cdest = (char *)dest;
  632.     char *csrc = (char *)src;
  633.     for (i=size-1;i>=0;i--) cdest[i] = csrc[i];
  634.       }      
  635.     }
  636.   return(dest);
  637. }
  638.  
  639.  
  640. /****************************************************************************
  641. prompte a dptr (to make it recently used)
  642. ****************************************************************************/
  643. void array_promote(char *array,int elsize,int element)
  644. {
  645.   char *p;
  646.   if (element == 0)
  647.     return;
  648.  
  649.   p = (char *)malloc(elsize);
  650.  
  651.   if (!p)
  652.     {
  653.       DEBUG(5,("Ahh! Can't malloc\n"));
  654.       return;
  655.     }
  656.   memcpy(p,array + element * elsize, elsize);
  657.   memmove(array + elsize,array,elsize*element);
  658.   memcpy(array,p,elsize);
  659.   free(p);
  660. }
  661.  
  662. enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
  663.  
  664. struct
  665. {
  666.   char *name;
  667.   int level;
  668.   int option;
  669.   int value;
  670.   int opttype;
  671. } socket_options[] = {
  672.   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
  673.   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
  674.   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
  675. #ifdef TCP_NODELAY
  676.   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
  677. #endif
  678. #ifdef IPTOS_LOWDELAY
  679.   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
  680. #endif
  681. #ifdef IPTOS_THROUGHPUT
  682.   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
  683. #endif
  684. #ifdef SO_SNDBUF
  685.   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
  686. #endif
  687. #ifdef SO_RCVBUF
  688.   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
  689. #endif
  690. #ifdef SO_SNDLOWAT
  691.   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
  692. #endif
  693. #ifdef SO_RCVLOWAT
  694.   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
  695. #endif
  696.   {NULL,0,0,0,0}};
  697.  
  698.     
  699.  
  700. /****************************************************************************
  701. set user socket options
  702. ****************************************************************************/
  703. void set_socket_options(int fd, char *options)
  704. {
  705.   string tok;
  706.  
  707.   while (next_token(&options,tok," \t,"))
  708.     {
  709.       int ret=0,i;
  710.       int value = 1;
  711.       char *p;
  712.       BOOL got_value = False;
  713.  
  714.       if ((p = strchr(tok,'=')))
  715.     {
  716.       *p = 0;
  717.       value = atoi(p+1);
  718.       got_value = True;
  719.     }
  720.  
  721.       for (i=0;socket_options[i].name;i++)
  722.     if (strequal(socket_options[i].name,tok))
  723.       break;
  724.  
  725.       if (!socket_options[i].name)
  726.     {
  727.       DEBUG(0,("Unknown socket option %s\n",tok));
  728.       continue;
  729.     }
  730.  
  731.       switch (socket_options[i].opttype)
  732.     {
  733.     case OPT_BOOL:
  734.     case OPT_INT:
  735.       ret = setsockopt(fd,socket_options[i].level,
  736.                socket_options[i].option,(char *)&value,sizeof(int));
  737.       break;
  738.  
  739.     case OPT_ON:
  740.       if (got_value)
  741.         DEBUG(0,("syntax error - %s does not take a value\n",tok));
  742.  
  743.       {
  744.         int on = socket_options[i].value;
  745.         ret = setsockopt(fd,socket_options[i].level,
  746.                  socket_options[i].option,(char *)&on,sizeof(int));
  747.       }
  748.       break;      
  749.     }
  750.       
  751.       if (ret != 0)
  752.     DEBUG(0,("Failed to set socket option %s\n",tok));
  753.     }
  754. }
  755.  
  756.  
  757.  
  758. /****************************************************************************
  759.   close the socket communication
  760. ****************************************************************************/
  761. void close_sockets(void )
  762. {
  763.   close(Client);
  764.   Client = 0;
  765. }
  766.  
  767. /****************************************************************************
  768.   return the date and time as a string
  769. ****************************************************************************/
  770. char *timestring(void )
  771. {
  772.   static char TimeBuf[100];
  773.   time_t t;
  774.   t = time(NULL);
  775. #ifdef NO_STRFTIME
  776.   strcpy(TimeBuf, asctime(LocalTime(&t,GMT_TO_LOCAL)));
  777. #elif defined(CLIX) || defined(CONVEX)
  778.   strftime(TimeBuf,100,"%m/%d/%y %I:%M:%S %p",LocalTime(&t,GMT_TO_LOCAL));
  779. #elif defined(AMPM)
  780.   strftime(TimeBuf,100,"%D %r",LocalTime(&t,GMT_TO_LOCAL));
  781. #elif defined(TZ_TIME)
  782.   {
  783.     strftime(TimeBuf,100,"%D:%T",LocalTime(&t,0));
  784.     sprintf(TimeBuf+strlen(TimeBuf)," %+03d%02d",
  785.         -TimeDiff(t)/(60*60),-(TimeDiff(t)/60)%60);
  786.   }
  787. #else
  788.   strftime(TimeBuf,100,"%D %T",LocalTime(&t,GMT_TO_LOCAL));
  789. #endif
  790.   return(TimeBuf);
  791. }
  792.  
  793. /****************************************************************************
  794. determine whether we are in the specified group
  795. ****************************************************************************/
  796. BOOL in_group(gid_t group, int current_gid, int ngroups, int *groups)
  797. {
  798.   int i;
  799.  
  800.   if (group == current_gid) return(True);
  801.  
  802.   for (i=0;i<ngroups;i++)
  803.     if (group == groups[i])
  804.       return(True);
  805.  
  806.   return(False);
  807. }
  808.  
  809. /****************************************************************************
  810. this is a safer strcpy(), meant to prevent core dumps when nasty things happen
  811. ****************************************************************************/
  812. char *StrCpy(char *dest,char *src)
  813. {
  814.   char *d = dest;
  815.  
  816. #if AJT
  817.   /* I don't want to get lazy with these ... */
  818.   if (!dest || !src) {
  819.     DEBUG(0,("ERROR: NULL StrCpy() called!\n"));
  820.     ajt_panic();
  821.   }
  822. #endif
  823.  
  824.   if (!dest) return(NULL);
  825.   if (!src) {
  826.     *dest = 0;
  827.     return(dest);
  828.   }
  829.   while ((*d++ = *src++)) ;
  830.   return(dest);
  831. }
  832.  
  833. /****************************************************************************
  834. line strncpy but always null terminates. Make sure there is room!
  835. ****************************************************************************/
  836. char *StrnCpy(char *dest,const char *src,int n)
  837. {
  838.   char *d = dest;
  839.   if (!dest) return(NULL);
  840.   if (!src) {
  841.     *dest = 0;
  842.     return(dest);
  843.   }
  844.   while (n-- && (*d++ = *src++)) ;
  845.   *d = 0;
  846.   return(dest);
  847. }
  848.  
  849.  
  850. /*******************************************************************
  851. copy an IP address from one buffer to another
  852. ********************************************************************/
  853. void putip(void *dest,void *src)
  854. {
  855.   memcpy(dest,src,4);
  856. }
  857.  
  858.  
  859. /****************************************************************************
  860. interpret the weird netbios "name". Return the name type
  861. ****************************************************************************/
  862. static int name_interpret(char *in,char *out)
  863. {
  864.   int ret;
  865.   int len = (*in++) / 2;
  866.  
  867.   *out=0;
  868.  
  869.   if (len > 30 || len<1) return(0);
  870.  
  871.   while (len--)
  872.     {
  873.       if (in[0] < 'A' || in[0] > 'P' || in[1] < 'A' || in[1] > 'P') {
  874.     *out = 0;
  875.     return(0);
  876.       }
  877.       *out = ((in[0]-'A')<<4) + (in[1]-'A');
  878.       in += 2;
  879.       out++;
  880.     }
  881.   *out = 0;
  882.   ret = out[-1];
  883.  
  884. #ifdef NETBIOS_SCOPE
  885.   /* Handle any scope names */
  886.   while(*in) 
  887.     {
  888.       *out++ = '.'; /* Scope names are separated by periods */
  889.       len = *(unsigned char *)in++;
  890.       StrnCpy(out, in, len);
  891.       out += len;
  892.       *out=0;
  893.       in += len;
  894.     }
  895. #endif
  896.   return(ret);
  897. }
  898.  
  899. /****************************************************************************
  900. mangle a name into netbios format
  901. ****************************************************************************/
  902. int name_mangle(char *In,char *Out,char name_type)
  903. {
  904.   fstring name;
  905.   char buf[20];
  906.   char *in = (char *)&buf[0];
  907.   char *out = (char *)Out;
  908.   char *p, *label;
  909.   int i;
  910.  
  911.   if (In[0] != '*') {
  912.     StrnCpy(name,In,sizeof(name)-1);
  913.     sprintf(buf,"%-15.15s%c",name,name_type);
  914.   } else {
  915.     buf[0]='*';
  916.     memset(&buf[1],0,16);
  917.   }
  918.  
  919.   *out++ = 32;
  920.   for (i=0;i<16;i++) {
  921.     char c = toupper(in[i]);
  922.     out[i*2] = (c>>4) + 'A';
  923.     out[i*2+1] = (c & 0xF) + 'A';
  924.   }
  925.   out[32]=0;
  926.   out += 32;
  927.   
  928.   label = scope;
  929.   while (*label)
  930.     {
  931.       p = strchr(label, '.');
  932.       if (p == 0)
  933.     p = label + strlen(label);
  934.       *out++ = p - label;
  935.       memcpy(out, label, p - label);
  936.       out += p - label;
  937.       label += p - label + (*p == '.');
  938.     }
  939.   *out = 0;
  940.   return(name_len(Out));
  941. }
  942.  
  943.  
  944. /*******************************************************************
  945.   check if a file exists
  946. ********************************************************************/
  947. BOOL file_exist(char *fname,struct stat *sbuf)
  948. {
  949.   struct stat st;
  950.   if (!sbuf) sbuf = &st;
  951.   
  952.   if (sys_stat(fname,sbuf) != 0) 
  953.     return(False);
  954.  
  955.   return(S_ISREG(sbuf->st_mode));
  956. }
  957.  
  958. /*******************************************************************
  959. check a files mod time
  960. ********************************************************************/
  961. time_t file_modtime(char *fname)
  962. {
  963.   struct stat st;
  964.   
  965.   if (sys_stat(fname,&st) != 0) 
  966.     return(0);
  967.  
  968.   return(st.st_mtime);
  969. }
  970.  
  971. /*******************************************************************
  972.   check if a directory exists
  973. ********************************************************************/
  974. BOOL directory_exist(char *dname,struct stat *st)
  975. {
  976.   struct stat st2;
  977.   if (!st) st = &st2;
  978.  
  979.   if (sys_stat(dname,st) != 0) 
  980.     return(False);
  981.  
  982.   return(S_ISDIR(st->st_mode));
  983. }
  984.  
  985. /*******************************************************************
  986. returns the size in bytes of the named file
  987. ********************************************************************/
  988. uint32 file_size(char *file_name)
  989. {
  990.   struct stat buf;
  991.   buf.st_size = 0;
  992.   sys_stat(file_name,&buf);
  993.   return(buf.st_size);
  994. }
  995.  
  996. /****************************************************************************
  997. check if it's a null mtime
  998. ****************************************************************************/
  999. static BOOL null_mtime(time_t mtime)
  1000. {
  1001.   if (mtime == 0 || mtime == 0xFFFFFFFF)
  1002.     return(True);
  1003.   return(False);
  1004. }
  1005.  
  1006. /*******************************************************************
  1007.   create a 16 bit dos packed date
  1008. ********************************************************************/
  1009. static uint16 make_dos_date1(time_t unixdate,struct tm *t)
  1010. {
  1011.   uint16 ret=0;
  1012.   ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
  1013.   ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
  1014.   return(ret);
  1015. }
  1016.  
  1017. /*******************************************************************
  1018.   create a 16 bit dos packed time
  1019. ********************************************************************/
  1020. static uint16 make_dos_time1(time_t unixdate,struct tm *t)
  1021. {
  1022.   uint16 ret=0;
  1023.   ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
  1024.   ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
  1025.   return(ret);
  1026. }
  1027.  
  1028. /*******************************************************************
  1029.   create a 32 bit dos packed date/time from some parameters
  1030.   This takes a GMT time and returns a packed localtime structure
  1031. ********************************************************************/
  1032. static uint32 make_dos_date(time_t unixdate)
  1033. {
  1034.   struct tm *t;
  1035.   uint32 ret=0;
  1036.  
  1037.   t = LocalTime(&unixdate,GMT_TO_LOCAL);
  1038.  
  1039.   ret = make_dos_date1(unixdate,t);
  1040.   ret = ((ret&0xFFFF)<<16) | make_dos_time1(unixdate,t);
  1041.  
  1042.   return(ret);
  1043. }
  1044.  
  1045. /*******************************************************************
  1046. put a dos date into a buffer (time/date format)
  1047. This takes GMT time and puts local time in the buffer
  1048. ********************************************************************/
  1049. void put_dos_date(char *buf,int offset,time_t unixdate)
  1050. {
  1051.   uint32 x = make_dos_date(unixdate);
  1052.   SIVAL(buf,offset,x);
  1053. }
  1054.  
  1055. /*******************************************************************
  1056. put a dos date into a buffer (date/time format)
  1057. This takes GMT time and puts local time in the buffer
  1058. ********************************************************************/
  1059. void put_dos_date2(char *buf,int offset,time_t unixdate)
  1060. {
  1061.   uint32 x = make_dos_date(unixdate);
  1062.   x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
  1063.   SIVAL(buf,offset,x);
  1064. }
  1065.  
  1066. /*******************************************************************
  1067. put a dos 32 bit "unix like" date into a buffer. This routine takes
  1068. GMT and converts it to LOCAL time before putting it (most SMBs assume
  1069. localtime for this sort of date)
  1070. ********************************************************************/
  1071. void put_dos_date3(char *buf,int offset,time_t unixdate)
  1072. {
  1073.   if (!null_mtime(unixdate))
  1074.     unixdate += GMT_TO_LOCAL*TimeDiff(unixdate);
  1075.   SIVAL(buf,offset,unixdate);
  1076. }
  1077.  
  1078. /*******************************************************************
  1079.   interpret a 32 bit dos packed date/time to some parameters
  1080. ********************************************************************/
  1081. static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
  1082. {
  1083.   uint32 p0,p1,p2,p3;
  1084.  
  1085.   p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
  1086.   p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
  1087.  
  1088.   *second = 2*(p0 & 0x1F);
  1089.   *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
  1090.   *hour = (p1>>3)&0xFF;
  1091.   *day = (p2&0x1F);
  1092.   *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
  1093.   *year = ((p3>>1)&0xFF) + 80;
  1094. }
  1095.  
  1096. /*******************************************************************
  1097.   create a unix date (int GMT) from a dos date (which is actually in
  1098.   localtime)
  1099. ********************************************************************/
  1100. time_t make_unix_date(void *date_ptr)
  1101. {
  1102.   uint32 dos_date=0;
  1103.   struct tm t;
  1104.   time_t ret;
  1105.  
  1106.   dos_date = IVAL(date_ptr,0);
  1107.  
  1108.   if (dos_date == 0) return(0);
  1109.   
  1110.   interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
  1111.              &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
  1112.   t.tm_wday = 1;
  1113.   t.tm_yday = 1;
  1114.   t.tm_isdst = -1;
  1115.   
  1116.   /* mktime() also does the local to GMT time conversion for us. XXXXX
  1117.      Do all unixes do this the same?? */
  1118.   ret = mktime(&t);
  1119.  
  1120.   return(ret);
  1121. }
  1122.  
  1123. /*******************************************************************
  1124. like make_unix_date() but the words are reversed
  1125. ********************************************************************/
  1126. time_t make_unix_date2(void *date_ptr)
  1127. {
  1128.   uint32 x,x2;
  1129.  
  1130.   x = IVAL(date_ptr,0);
  1131.   x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
  1132.   SIVAL(&x,0,x2);
  1133.  
  1134.   return(make_unix_date((void *)&x));
  1135. }
  1136.  
  1137. /*******************************************************************
  1138.   create a unix GMT date from a dos date in 32 bit "unix like" format
  1139. these generally arrive as localtimes, with corresponding DST
  1140. ********************************************************************/
  1141. time_t make_unix_date3(void *date_ptr)
  1142. {
  1143.   time_t t = IVAL(date_ptr,0);
  1144.   if (!null_mtime(t))
  1145.     t += LOCAL_TO_GMT*TimeDiff(t);
  1146.   return(t);
  1147. }
  1148.  
  1149. /*******************************************************************
  1150. return a string representing an attribute for a file
  1151. ********************************************************************/
  1152. char *attrib_string(int mode)
  1153. {
  1154.   static char attrstr[10];
  1155.  
  1156.   attrstr[0] = 0;
  1157.  
  1158.   if (mode & aVOLID) strcat(attrstr,"V");
  1159.   if (mode & aDIR) strcat(attrstr,"D");
  1160.   if (mode & aARCH) strcat(attrstr,"A");
  1161.   if (mode & aHIDDEN) strcat(attrstr,"H");
  1162.   if (mode & aSYSTEM) strcat(attrstr,"S");
  1163.   if (mode & aRONLY) strcat(attrstr,"R");      
  1164.  
  1165.   return(attrstr);
  1166. }
  1167.  
  1168.  
  1169. /*******************************************************************
  1170.   case insensitive string compararison
  1171. ********************************************************************/
  1172. int StrCaseCmp(char *s, char *t)
  1173. {
  1174.   for (; tolower(*s) == tolower(*t); ++s, ++t)
  1175.     if (!*s) return 0;
  1176.  
  1177.   return tolower(*s) - tolower(*t);
  1178. }
  1179.  
  1180. /*******************************************************************
  1181.   case insensitive string compararison, length limited
  1182. ********************************************************************/
  1183. int StrnCaseCmp(char *s, char *t, int n)
  1184. {
  1185.   while (n-- && *s && *t) {
  1186.     if (tolower(*s) != tolower(*t)) return(tolower(*s) - tolower(*t));
  1187.     s++; t++;
  1188.   }
  1189.   if (n) return(tolower(*s) - tolower(*t));
  1190.  
  1191.   return(0);
  1192. }
  1193.  
  1194. /*******************************************************************
  1195.   compare 2 strings 
  1196. ********************************************************************/
  1197. BOOL strequal(char *s1,char *s2)
  1198. {
  1199.   if (!s1 || !s2) return(False);
  1200.   
  1201.   return(StrCaseCmp(s1,s2)==0);
  1202. }
  1203.  
  1204. /*******************************************************************
  1205.   compare 2 strings up to and including the nth char.
  1206.   ******************************************************************/
  1207. BOOL strnequal(char *s1,char *s2,int n)
  1208. {
  1209.   if (!s1 || !s2 || !n) return(False);
  1210.   
  1211.   return(StrnCaseCmp(s1,s2,n)==0);
  1212. }
  1213.  
  1214. /*******************************************************************
  1215.   compare 2 strings (case sensitive)
  1216. ********************************************************************/
  1217. BOOL strcsequal(char *s1,char *s2)
  1218. {
  1219.   if (!s1 || !s2) return(False);
  1220.   
  1221.   return(strcmp(s1,s2)==0);
  1222. }
  1223.  
  1224.  
  1225. /*******************************************************************
  1226.   convert a string to lower case
  1227. ********************************************************************/
  1228. void strlower(char *s)
  1229. {
  1230.   while (*s)
  1231.     {
  1232. #ifdef KANJI
  1233.     if (is_shift_jis (*s)) {
  1234.         s += 2;
  1235.     } else if (is_kana (*s)) {
  1236.         s++;
  1237.     } else {
  1238.         if (isupper(*s))
  1239.         *s = tolower(*s);
  1240.         s++;
  1241.     }
  1242. #else
  1243.       if (isupper(*s))
  1244.       *s = tolower(*s);
  1245.       s++;
  1246. #endif /* KANJI */
  1247.     }
  1248. }
  1249.  
  1250. /*******************************************************************
  1251.   convert a string to upper case
  1252. ********************************************************************/
  1253. void strupper(char *s)
  1254. {
  1255.   while (*s)
  1256.     {
  1257. #ifdef KANJI
  1258.     if (is_shift_jis (*s)) {
  1259.         s += 2;
  1260.     } else if (is_kana (*s)) {
  1261.         s++;
  1262.     } else {
  1263.         if (islower(*s))
  1264.         *s = toupper(*s);
  1265.         s++;
  1266.     }
  1267. #else
  1268.       if (islower(*s))
  1269.     *s = toupper(*s);
  1270.       s++;
  1271. #endif
  1272.     }
  1273. }
  1274.  
  1275. /*******************************************************************
  1276.   convert a string to "normal" form
  1277. ********************************************************************/
  1278. void strnorm(char *s)
  1279. {
  1280.   if (case_default == CASE_UPPER)
  1281.     strupper(s);
  1282.   else
  1283.     strlower(s);
  1284. }
  1285.  
  1286. /*******************************************************************
  1287. check if a string is in "normal" case
  1288. ********************************************************************/
  1289. BOOL strisnormal(char *s)
  1290. {
  1291.   if (case_default == CASE_UPPER)
  1292.     return(!strhaslower(s));
  1293.  
  1294.   return(!strhasupper(s));
  1295. }
  1296.  
  1297.  
  1298. /****************************************************************************
  1299.   string replace
  1300. ****************************************************************************/
  1301. void string_replace(char *s,char oldc,char newc)
  1302. {
  1303.   while (*s)
  1304.     {
  1305. #ifdef KANJI
  1306.     if (is_shift_jis (*s)) {
  1307.         s += 2;
  1308.     } else if (is_kana (*s)) {
  1309.         s++;
  1310.     } else {
  1311.         if (oldc == *s)
  1312.         *s = newc;
  1313.         s++;
  1314.     }
  1315. #else
  1316.       if (oldc == *s)
  1317.     *s = newc;
  1318.       s++;
  1319. #endif /* KANJI */
  1320.     }
  1321. }
  1322.  
  1323. /****************************************************************************
  1324.   make a file into unix format
  1325. ****************************************************************************/
  1326. void unix_format(char *fname)
  1327. {
  1328.   pstring namecopy;
  1329.   string_replace(fname,'\\','/');
  1330. #ifndef KANJI
  1331.   dos2unix_format(fname, True);
  1332. #endif /* KANJI */
  1333.  
  1334.   if (*fname == '/')
  1335.     {
  1336.       strcpy(namecopy,fname);
  1337.       strcpy(fname,".");
  1338.       strcat(fname,namecopy);
  1339.     }  
  1340. }
  1341.  
  1342. /****************************************************************************
  1343.   make a file into dos format
  1344. ****************************************************************************/
  1345. void dos_format(char *fname)
  1346. {
  1347. #ifndef KANJI
  1348.   unix2dos_format(fname, True);
  1349. #endif /* KANJI */
  1350.   string_replace(fname,'/','\\');
  1351. }
  1352.  
  1353.  
  1354. /*******************************************************************
  1355.   show a smb message structure
  1356. ********************************************************************/
  1357. void show_msg(char *buf)
  1358. {
  1359.   int i;
  1360.   int bcc=0;
  1361.   if (DEBUGLEVEL < 5)
  1362.     return;
  1363.  
  1364.   DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
  1365.       smb_len(buf),
  1366.       (int)CVAL(buf,smb_com),
  1367.       (int)CVAL(buf,smb_rcls),
  1368.       (int)CVAL(buf,smb_reh),
  1369.       (int)SVAL(buf,smb_err),
  1370.       (int)CVAL(buf,smb_flg),
  1371.       (int)SVAL(buf,smb_flg2)));
  1372.   DEBUG(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\nsmt_wct=%d\n",
  1373.       (int)SVAL(buf,smb_tid),
  1374.       (int)SVAL(buf,smb_pid),
  1375.       (int)SVAL(buf,smb_uid),
  1376.       (int)SVAL(buf,smb_mid),
  1377.       (int)CVAL(buf,smb_wct)));
  1378.   for (i=0;i<(int)CVAL(buf,smb_wct);i++)
  1379.     DEBUG(5,("smb_vwv[%d]=%d (0x%X)\n",i,
  1380.       SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
  1381.   bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
  1382.   DEBUG(5,("smb_bcc=%d\n",bcc));
  1383.   if (DEBUGLEVEL < 10)
  1384.     return;
  1385.   for (i=0;i<MIN(bcc,128);i++)
  1386.     DEBUG(10,("%X ",CVAL(smb_buf(buf),i)));
  1387.   DEBUG(10,("\n"));  
  1388. }
  1389.  
  1390. /*******************************************************************
  1391.   return the length of an smb packet
  1392. ********************************************************************/
  1393. int smb_len(char *buf)
  1394. {
  1395.   return( PVAL(buf,3) | (PVAL(buf,2)<<8) | ((PVAL(buf,1)&1)<<16) );
  1396. }
  1397.  
  1398. /*******************************************************************
  1399.   set the length of an smb packet
  1400. ********************************************************************/
  1401. void _smb_setlen(char *buf,int len)
  1402. {
  1403.   buf[0] = 0;
  1404.   buf[1] = (len&0x10000)>>16;
  1405.   buf[2] = (len&0xFF00)>>8;
  1406.   buf[3] = len&0xFF;
  1407. }
  1408.  
  1409. /*******************************************************************
  1410.   set the length and marker of an smb packet
  1411. ********************************************************************/
  1412. void smb_setlen(char *buf,int len)
  1413. {
  1414.   _smb_setlen(buf,len);
  1415.  
  1416.   CVAL(buf,4) = 0xFF;
  1417.   CVAL(buf,5) = 'S';
  1418.   CVAL(buf,6) = 'M';
  1419.   CVAL(buf,7) = 'B';
  1420. }
  1421.  
  1422. /*******************************************************************
  1423.   setup the word count and byte count for a smb message
  1424. ********************************************************************/
  1425. int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
  1426. {
  1427.   if (zero)
  1428.     bzero(buf + smb_size,num_words*2 + num_bytes);
  1429.   CVAL(buf,smb_wct) = num_words;
  1430.   SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);  
  1431.   smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
  1432.   return (smb_size + num_words*2 + num_bytes);
  1433. }
  1434.  
  1435. /*******************************************************************
  1436. return the number of smb words
  1437. ********************************************************************/
  1438. int smb_numwords(char *buf)
  1439. {
  1440.   return (CVAL(buf,smb_wct));
  1441. }
  1442.  
  1443. /*******************************************************************
  1444. return the size of the smb_buf region of a message
  1445. ********************************************************************/
  1446. int smb_buflen(char *buf)
  1447. {
  1448.   return(SVAL(buf,smb_vwv0 + smb_numwords(buf)*2));
  1449. }
  1450.  
  1451. /*******************************************************************
  1452.   return a pointer to the smb_buf data area
  1453. ********************************************************************/
  1454. int smb_buf_ofs(char *buf)
  1455. {
  1456.   return (smb_size + CVAL(buf,smb_wct)*2);
  1457. }
  1458.  
  1459. /*******************************************************************
  1460.   return a pointer to the smb_buf data area
  1461. ********************************************************************/
  1462. char *smb_buf(char *buf)
  1463. {
  1464.   return (buf + smb_buf_ofs(buf));
  1465. }
  1466.  
  1467. /*******************************************************************
  1468. return the SMB offset into an SMB buffer
  1469. ********************************************************************/
  1470. int smb_offset(char *p,char *buf)
  1471. {
  1472.   return(PTR_DIFF(p,buf+4));
  1473. }
  1474.  
  1475.  
  1476. /*******************************************************************
  1477. skip past some strings in a buffer
  1478. ********************************************************************/
  1479. char *skip_string(char *buf,int n)
  1480. {
  1481.   while (n--)
  1482.     buf += strlen(buf) + 1;
  1483.   return(buf);
  1484. }
  1485.  
  1486. /*******************************************************************
  1487. trim the specified elements off the front and back of a string
  1488. ********************************************************************/
  1489. BOOL trim_string(char *s,char *front,char *back)
  1490. {
  1491.   BOOL ret = False;
  1492.   while (front && *front && strncmp(s,front,strlen(front)) == 0)
  1493.     {
  1494.       char *p = s;
  1495.       ret = True;
  1496.       while (1)
  1497.     {
  1498.       if (!(*p = p[strlen(front)]))
  1499.         break;
  1500.       p++;
  1501.     }
  1502.     }
  1503.   while (back && *back && strlen(s) >= strlen(back) && 
  1504.      (strncmp(s+strlen(s)-strlen(back),back,strlen(back))==0))  
  1505.     {
  1506.       ret = True;
  1507.       s[strlen(s)-strlen(back)] = 0;
  1508.     }
  1509.   return(ret);
  1510. }
  1511.  
  1512.  
  1513. /*******************************************************************
  1514. reduce a file name, removing .. elements.
  1515. ********************************************************************/
  1516. void dos_clean_name(char *s)
  1517. {
  1518.   char *p=NULL;
  1519.  
  1520.   DEBUG(3,("dos_clean_name [%s]\n",s));
  1521.  
  1522.   /* remove any double slashes */
  1523.   string_sub(s, "\\\\", "\\");
  1524.  
  1525.   while ((p = strstr(s,"\\..\\")) != NULL)
  1526.     {
  1527.       pstring s1;
  1528.  
  1529.       *p = 0;
  1530.       strcpy(s1,p+3);
  1531.  
  1532.       if ((p=strrchr(s,'\\')) != NULL)
  1533.     *p = 0;
  1534.       else
  1535.     *s = 0;
  1536.       strcat(s,s1);
  1537.     }  
  1538.  
  1539.   trim_string(s,NULL,"\\..");
  1540.  
  1541.   string_sub(s, "\\.\\", "\\");
  1542. }
  1543.  
  1544. /*******************************************************************
  1545. reduce a file name, removing .. elements. 
  1546. ********************************************************************/
  1547. void unix_clean_name(char *s)
  1548. {
  1549.   char *p=NULL;
  1550.  
  1551.   DEBUG(3,("unix_clean_name [%s]\n",s));
  1552.  
  1553.   /* remove any double slashes */
  1554.   string_sub(s, "//","/");
  1555.  
  1556.   while ((p = strstr(s,"/../")) != NULL)
  1557.     {
  1558.       pstring s1;
  1559.  
  1560.       *p = 0;
  1561.       strcpy(s1,p+3);
  1562.  
  1563.       if ((p=strrchr(s,'/')) != NULL)
  1564.     *p = 0;
  1565.       else
  1566.     *s = 0;
  1567.       strcat(s,s1);
  1568.     }  
  1569.  
  1570.   trim_string(s,NULL,"/..");
  1571. }
  1572.  
  1573.  
  1574. /*******************************************************************
  1575. a wrapper for the normal chdir() function
  1576. ********************************************************************/
  1577. int ChDir(char *path)
  1578. {
  1579.   int res;
  1580.   static pstring LastDir="";
  1581.  
  1582.   if (strcsequal(path,".")) return(0);
  1583.  
  1584.   if (*path == '/' && strcsequal(LastDir,path)) return(0);
  1585.   DEBUG(3,("chdir to %s\n",path));
  1586.   res = sys_chdir(path);
  1587.   if (!res)
  1588.     strcpy(LastDir,path);
  1589.   return(res);
  1590. }
  1591.  
  1592.  
  1593. /*******************************************************************
  1594.   return the absolute current directory path. A dumb version.
  1595. ********************************************************************/
  1596. static char *Dumb_GetWd(char *s)
  1597. {
  1598. #ifdef USE_GETCWD
  1599.     return ((char *)getcwd(s,sizeof(pstring)));
  1600. #else
  1601.     return ((char *)getwd(s));
  1602. #endif
  1603. }
  1604.  
  1605.  
  1606. /* number of list structures for a caching GetWd function. */
  1607. #define MAX_GETWDCACHE (50)
  1608.  
  1609. struct
  1610. {
  1611.   ino_t inode;
  1612.   dev_t dev;
  1613.   char *text;
  1614.   BOOL valid;
  1615. } ino_list[MAX_GETWDCACHE];
  1616.  
  1617. BOOL use_getwd_cache=True;
  1618.  
  1619. /*******************************************************************
  1620.   return the absolute current directory path
  1621. ********************************************************************/
  1622. char *GetWd(char *str)
  1623. {
  1624.   pstring s;
  1625.   static BOOL getwd_cache_init = False;
  1626.   struct stat st, st2;
  1627.   int i;
  1628.  
  1629.   *s = 0;
  1630.  
  1631.   if (!use_getwd_cache)
  1632.     return(Dumb_GetWd(str));
  1633.  
  1634.   /* init the cache */
  1635.   if (!getwd_cache_init)
  1636.     {
  1637.       getwd_cache_init = True;
  1638.       for (i=0;i<MAX_GETWDCACHE;i++)
  1639.     {
  1640.       string_init(&ino_list[i].text,"");
  1641.       ino_list[i].valid = False;
  1642.     }
  1643.     }
  1644.  
  1645.   /*  Get the inode of the current directory, if this doesn't work we're
  1646.       in trouble :-) */
  1647.  
  1648.   if (stat(".",&st) == -1) 
  1649.     {
  1650.       DEBUG(0,("Very strange, couldn't stat \".\"\n"));
  1651.       return(Dumb_GetWd(str));
  1652.     }
  1653.  
  1654.  
  1655.   for (i=0; i<MAX_GETWDCACHE; i++)
  1656.     if (ino_list[i].valid)
  1657.       {
  1658.  
  1659.     /*  If we have found an entry with a matching inode and dev number
  1660.         then find the inode number for the directory in the cached string.
  1661.         If this agrees with that returned by the stat for the current
  1662.         directory then all is o.k. (but make sure it is a directory all
  1663.         the same...) */
  1664.       
  1665.     if (st.st_ino == ino_list[i].inode &&
  1666.         st.st_dev == ino_list[i].dev)
  1667.       {
  1668.         if (stat(ino_list[i].text,&st2) == 0)
  1669.           {
  1670.         if (st.st_ino == st2.st_ino &&
  1671.             st.st_dev == st2.st_dev &&
  1672.             (st2.st_mode & S_IFMT) == S_IFDIR)
  1673.           {
  1674.             strcpy (str, ino_list[i].text);
  1675.  
  1676.             /* promote it for future use */
  1677.             array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
  1678.             return (str);
  1679.           }
  1680.         else
  1681.           {
  1682.             /*  If the inode is different then something's changed, 
  1683.             scrub the entry and start from scratch. */
  1684.             ino_list[i].valid = False;
  1685.           }
  1686.           }
  1687.       }
  1688.       }
  1689.  
  1690.  
  1691.   /*  We don't have the information to hand so rely on traditional methods.
  1692.       The very slow getcwd, which spawns a process on some systems, or the
  1693.       not quite so bad getwd. */
  1694.  
  1695.   if (!Dumb_GetWd(s))
  1696.     {
  1697.       DEBUG(0,("Getwd failed, errno %d\n",errno));
  1698.       return (NULL);
  1699.     }
  1700.  
  1701.   strcpy(str,s);
  1702.  
  1703.   DEBUG(5,("GetWd %s, inode %d, dev %x\n",s,(int)st.st_ino,(int)st.st_dev));
  1704.  
  1705.   /* add it to the cache */
  1706.   i = MAX_GETWDCACHE - 1;
  1707.   string_set(&ino_list[i].text,s);
  1708.   ino_list[i].dev = st.st_dev;
  1709.   ino_list[i].inode = st.st_ino;
  1710.   ino_list[i].valid = True;
  1711.  
  1712.   /* put it at the top of the list */
  1713.   array_promote((char *)&ino_list[0],sizeof(ino_list[0]),i);
  1714.  
  1715.   return (str);
  1716. }
  1717.  
  1718.  
  1719.  
  1720. /*******************************************************************
  1721. reduce a file name, removing .. elements and checking that 
  1722. it is below dir in the heirachy. This uses GetWd() and so must be run
  1723. on the system that has the referenced file system.
  1724.  
  1725. widelinks are allowed if widelinks is true
  1726. ********************************************************************/
  1727. BOOL reduce_name(char *s,char *dir,BOOL widelinks)
  1728. {
  1729. #ifndef REDUCE_PATHS
  1730.   return True;
  1731. #else
  1732.   pstring dir2;
  1733.   pstring wd;
  1734.   pstring basename;
  1735.   pstring newname;
  1736.   char *p=NULL;
  1737.   BOOL relative = (*s != '/');
  1738.  
  1739.   *dir2 = *wd = *basename = *newname = 0;
  1740.  
  1741.   if (widelinks)
  1742.     {
  1743.       unix_clean_name(s);
  1744.       /* can't have a leading .. */
  1745.       if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/'))
  1746.     {
  1747.       DEBUG(3,("Illegal file name? (%s)\n",s));
  1748.       return(False);
  1749.     }
  1750.       return(True);
  1751.     }
  1752.   
  1753.   DEBUG(3,("reduce_name [%s] [%s]\n",s,dir));
  1754.  
  1755.   /* remove any double slashes */
  1756.   string_sub(s,"//","/");
  1757.  
  1758.   strcpy(basename,s);
  1759.   p = strrchr(basename,'/');
  1760.  
  1761.   if (!p)
  1762.     return(True);
  1763.  
  1764.   if (!GetWd(wd))
  1765.     {
  1766.       DEBUG(0,("couldn't getwd for %s %s\n",s,dir));
  1767.       return(False);
  1768.     }
  1769.  
  1770.   if (ChDir(dir) != 0)
  1771.     {
  1772.       DEBUG(0,("couldn't chdir to %s\n",dir));
  1773.       return(False);
  1774.     }
  1775.  
  1776.   if (!GetWd(dir2))
  1777.     {
  1778.       DEBUG(0,("couldn't getwd for %s\n",dir));
  1779.       ChDir(wd);
  1780.       return(False);
  1781.     }
  1782.  
  1783.  
  1784.     if (p && (p != basename))
  1785.       {
  1786.     *p = 0;
  1787.     if (strcmp(p+1,".")==0)
  1788.       p[1]=0;
  1789.     if (strcmp(p+1,"..")==0)
  1790.       *p = '/';
  1791.       }
  1792.  
  1793.   if (ChDir(basename) != 0)
  1794.     {
  1795.       ChDir(wd);
  1796.       DEBUG(3,("couldn't chdir for %s %s basename=%s\n",s,dir,basename));
  1797.       return(False);
  1798.     }
  1799.  
  1800.   if (!GetWd(newname))
  1801.     {
  1802.       ChDir(wd);
  1803.       DEBUG(2,("couldn't get wd for %s %s\n",s,dir2));
  1804.       return(False);
  1805.     }
  1806.  
  1807.   if (p && (p != basename))
  1808.     {
  1809.       strcat(newname,"/");
  1810.       strcat(newname,p+1);
  1811.     }
  1812.  
  1813.   {
  1814.     int l = strlen(dir2);    
  1815.     if (dir2[l-1] == '/')
  1816.       l--;
  1817.  
  1818.     if (strncmp(newname,dir2,l) != 0)
  1819.       {
  1820.     ChDir(wd);
  1821.     DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,l));
  1822.     return(False);
  1823.       }
  1824.  
  1825.     if (relative)
  1826.       {
  1827.     if (newname[l] == '/')
  1828.       strcpy(s,newname + l + 1);
  1829.     else
  1830.       strcpy(s,newname+l);
  1831.       }
  1832.     else
  1833.       strcpy(s,newname);
  1834.   }
  1835.  
  1836.   ChDir(wd);
  1837.  
  1838.   if (strlen(s) == 0)
  1839.     strcpy(s,"./");
  1840.  
  1841.   DEBUG(3,("reduced to %s\n",s));
  1842.   return(True);
  1843. #endif
  1844. }
  1845.  
  1846. /****************************************************************************
  1847. expand some *s 
  1848. ****************************************************************************/
  1849. static void expand_one(char *Mask,int len)
  1850. {
  1851.   char *p1;
  1852.   while ((p1 = strchr(Mask,'*')) != NULL)
  1853.     {
  1854.       int lfill = (len+1) - strlen(Mask);
  1855.       int l1= (p1 - Mask);
  1856.       pstring tmp;
  1857.       strcpy(tmp,Mask);  
  1858.       memset(tmp+l1,'?',lfill);
  1859.       strcpy(tmp + l1 + lfill,Mask + l1 + 1);    
  1860.       strcpy(Mask,tmp);      
  1861.     }
  1862. }
  1863.  
  1864. /****************************************************************************
  1865. expand a wildcard expression, replacing *s with ?s
  1866. ****************************************************************************/
  1867. void expand_mask(char *Mask,BOOL doext)
  1868. {
  1869.   pstring mbeg,mext;
  1870.   pstring dirpart;
  1871.   pstring filepart;
  1872.   BOOL hasdot = False;
  1873.   char *p1;
  1874.   BOOL absolute = (*Mask == '\\');
  1875.  
  1876.   *mbeg = *mext = *dirpart = *filepart = 0;
  1877.  
  1878.   /* parse the directory and filename */
  1879.   if (strchr(Mask,'\\'))
  1880.     dirname_dos(Mask,dirpart);
  1881.  
  1882.   filename_dos(Mask,filepart);
  1883.  
  1884.   strcpy(mbeg,filepart);
  1885.   if ((p1 = strchr(mbeg,'.')) != NULL)
  1886.     {
  1887.       hasdot = True;
  1888.       *p1 = 0;
  1889.       p1++;
  1890.       strcpy(mext,p1);
  1891.     }
  1892.   else
  1893.     {
  1894.       strcpy(mext,"");
  1895.       if (strlen(mbeg) > 8)
  1896.     {
  1897.       strcpy(mext,mbeg + 8);
  1898.       mbeg[8] = 0;
  1899.     }
  1900.     }
  1901.  
  1902.   if (*mbeg == 0)
  1903.     strcpy(mbeg,"????????");
  1904.   if ((*mext == 0) && doext && !hasdot)
  1905.     strcpy(mext,"???");
  1906.  
  1907.   /* expand *'s */
  1908.   expand_one(mbeg,8);
  1909.   if (*mext)
  1910.     expand_one(mext,3);
  1911.  
  1912.   strcpy(Mask,dirpart);
  1913.   if (*dirpart || absolute) strcat(Mask,"\\");
  1914.   strcat(Mask,mbeg);
  1915.   strcat(Mask,".");
  1916.   strcat(Mask,mext);
  1917.  
  1918.   DEBUG(6,("Mask expanded to [%s]\n",Mask));
  1919. }  
  1920.  
  1921.  
  1922. /****************************************************************************
  1923. does a string have any uppercase chars in it?
  1924. ****************************************************************************/
  1925. BOOL strhasupper(char *s)
  1926. {
  1927.   while (*s) 
  1928.     {
  1929. #ifdef KANJI
  1930.     if (is_shift_jis (*s)) {
  1931.         s += 2;
  1932.     } else if (is_kana (*s)) {
  1933.         s++;
  1934.     } else {
  1935.         if (isupper(*s)) return(True);
  1936.         s++;
  1937.     }
  1938. #else 
  1939.       if (isupper(*s)) return(True);
  1940.       s++;
  1941. #endif /* KANJI */
  1942.     }
  1943.   return(False);
  1944. }
  1945.  
  1946. /****************************************************************************
  1947. does a string have any lowercase chars in it?
  1948. ****************************************************************************/
  1949. BOOL strhaslower(char *s)
  1950. {
  1951.   while (*s) 
  1952.     {
  1953. #ifdef KANJI
  1954.     if (is_shift_jis (*s)) {
  1955.         s += 2;
  1956.     } else if (is_kana (*s)) {
  1957.         s++;
  1958.     } else {
  1959.         if (islower(*s)) return(True);
  1960.         s++;
  1961.     }
  1962. #else 
  1963.       if (islower(*s)) return(True);
  1964.       s++;
  1965. #endif /* KANJI */
  1966.     }
  1967.   return(False);
  1968. }
  1969.  
  1970. /****************************************************************************
  1971. find the number of chars in a string
  1972. ****************************************************************************/
  1973. int count_chars(char *s,char c)
  1974. {
  1975.   int count=0;
  1976.   while (*s) 
  1977.     {
  1978.       if (*s == c)
  1979.     count++;
  1980.       s++;
  1981.     }
  1982.   return(count);
  1983. }
  1984.  
  1985.  
  1986. /****************************************************************************
  1987.   make a dir struct
  1988. ****************************************************************************/
  1989. void make_dir_struct(char *buf,char *mask,char *fname,unsigned int size,int mode,time_t date)
  1990. {  
  1991.   char *p;
  1992.   pstring mask2;
  1993.  
  1994.   strcpy(mask2,mask);
  1995.  
  1996.   if ((mode & aDIR) != 0)
  1997.     size = 0;
  1998.  
  1999.   memset(buf+1,' ',11);
  2000.   if ((p = strchr(mask2,'.')) != NULL)
  2001.     {
  2002.       *p = 0;
  2003.       memcpy(buf+1,mask2,MIN(strlen(mask2),8));
  2004.       memcpy(buf+9,p+1,MIN(strlen(p+1),3));
  2005.       *p = '.';
  2006.     }
  2007.   else
  2008.     memcpy(buf+1,mask2,MIN(strlen(mask2),11));
  2009.  
  2010.   bzero(buf+21,DIR_STRUCT_SIZE-21);
  2011.   CVAL(buf,21) = mode;
  2012.   put_dos_date(buf,22,date);
  2013.   SSVAL(buf,26,size & 0xFFFF);
  2014.   SSVAL(buf,28,size >> 16);
  2015.   StrnCpy(buf+30,fname,12);
  2016.   if (!case_sensitive)
  2017.     strupper(buf+30);
  2018.   DEBUG(8,("put name [%s] into dir struct\n",buf+30));
  2019. }
  2020.  
  2021.  
  2022. /*******************************************************************
  2023. close the low 3 fd's and open dev/null in their place
  2024. ********************************************************************/
  2025. void close_low_fds(void)
  2026. {
  2027.   int fd;
  2028.   int i;
  2029.   close(0); close(1); close(2);
  2030.   /* try and use up these file descriptors, so silly
  2031.      library routines writing to stdout etc won't cause havoc */
  2032.   for (i=0;i<3;i++) {
  2033.     fd = open("/dev/null",O_RDWR,0);
  2034.     if (fd < 0) fd = open("/dev/null",O_WRONLY,0);
  2035.     if (fd < 0) {
  2036.       DEBUG(0,("Can't open /dev/null\n"));
  2037.       return;
  2038.     }
  2039.     if (fd != i) {
  2040.       DEBUG(0,("Didn't get file descriptor %d\n",i));
  2041.       return;
  2042.     }
  2043.   }
  2044. }
  2045.  
  2046.  
  2047. /****************************************************************************
  2048. write to a socket
  2049. ****************************************************************************/
  2050. int write_socket(int fd,char *buf,int len)
  2051. {
  2052.   int ret=0;
  2053.  
  2054.   if (passive)
  2055.     return(len);
  2056.   DEBUG(6,("write_socket(%d,%d)\n",fd,len));
  2057.   ret = write_data(fd,buf,len);
  2058.       
  2059.   DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
  2060.   return(ret);
  2061. }
  2062.  
  2063. /****************************************************************************
  2064. read from a socket
  2065. ****************************************************************************/
  2066. int read_udp_socket(int fd,char *buf,int len)
  2067. {
  2068.   int ret;
  2069.   struct sockaddr sock;
  2070.   int socklen;
  2071.   
  2072.   socklen = sizeof(sock);
  2073.   bzero((char *)&sock,socklen);
  2074.   bzero((char *)&lastip,sizeof(lastip));
  2075.   ret = recvfrom(fd,buf,len,0,&sock,&socklen);
  2076.   if (ret <= 0)
  2077.     {
  2078.       DEBUG(2,("read socket failed. ERRNO=%d\n",errno));
  2079.       return(0);
  2080.     }
  2081.  
  2082.   lastip = *(struct in_addr *) &sock.sa_data[2];
  2083.   lastport = ntohs(((struct sockaddr_in *)&sock)->sin_port);
  2084.  
  2085.   return(ret);
  2086. }
  2087.  
  2088. /****************************************************************************
  2089. Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
  2090. else
  2091. if SYSV use O_NDELAY
  2092. if BSD use FNDELAY
  2093. ****************************************************************************/
  2094. int set_blocking(int fd, BOOL set)
  2095. {
  2096. int val;
  2097. #ifdef O_NONBLOCK
  2098. #define FLAG_TO_SET O_NONBLOCK
  2099. #else
  2100. #ifdef SYSV
  2101. #define FLAG_TO_SET O_NDELAY
  2102. #else /* BSD */
  2103. #define FLAG_TO_SET FNDELAY
  2104. #endif
  2105. #endif
  2106.  
  2107.   if((val = fcntl(fd, F_GETFL, 0))==-1)
  2108.     return -1;
  2109.   if(set) /* Turn blocking on - ie. clear nonblock flag */
  2110.     val &= ~FLAG_TO_SET;
  2111.   else
  2112.     val |= FLAG_TO_SET;
  2113.   return fcntl( fd, F_SETFL, val);
  2114. #undef FLAG_TO_SET
  2115. }
  2116.  
  2117.  
  2118. /****************************************************************************
  2119. Calculate the difference in timeout values. Return 1 if val1 > val2,
  2120. 0 if val1 == val2, -1 if val1 < val2. Stores result in retval. retval
  2121. may be == val1 or val2
  2122. ****************************************************************************/
  2123. static int tval_sub( struct timeval *retval, struct timeval *val1, struct timeval *val2)
  2124. {
  2125.   int usecdiff = val1->tv_usec - val2->tv_usec;
  2126.   int secdiff = val1->tv_sec - val2->tv_sec;
  2127.   if(usecdiff < 0) {
  2128.     usecdiff = 1000000 + usecdiff;
  2129.     secdiff--;
  2130.   }
  2131.   retval->tv_sec = secdiff;
  2132.   retval->tv_usec = usecdiff;
  2133.   if(secdiff < 0)
  2134.     return -1;
  2135.   if(secdiff > 0)
  2136.     return 1;
  2137.   return (usecdiff < 0 ) ? -1 : ((usecdiff > 0 ) ? 1 : 0);
  2138. }
  2139.  
  2140. /****************************************************************************
  2141. read data from a device with a timout in msec.
  2142. mincount = if timeout, minimum to read before returning
  2143. maxcount = number to be read.
  2144. ****************************************************************************/
  2145. int read_with_timeout(int fd,char *buf,int mincnt,int maxcnt,long time_out,BOOL exact)
  2146. {
  2147.   fd_set fds;
  2148.   int selrtn;
  2149.   int readret;
  2150.   int nread = 0;
  2151.   struct timeval timeout, tval1, tval2, tvaldiff;
  2152.  
  2153.   /* just checking .... */
  2154.   if (maxcnt <= 0) return(0);
  2155.  
  2156.   if(time_out == -2)
  2157.     time_out = DEFAULT_PIPE_TIMEOUT;
  2158.  
  2159.   /* Blocking read */
  2160.   if(time_out < 0) {
  2161.     if (mincnt == 0) mincnt = maxcnt;
  2162.  
  2163.     while (nread < mincnt)
  2164.       {
  2165.     readret = read(fd, buf + nread, maxcnt - nread);
  2166.     if (readret <= 0) return(nread);
  2167.     nread += readret;
  2168.       }
  2169.     return(nread);
  2170.   }
  2171.   
  2172.   /* Non blocking read */
  2173.   if(time_out == 0) {
  2174.     set_blocking(fd, False);
  2175.     nread = read_data(fd, buf, mincnt);
  2176.     if (nread < maxcnt)
  2177.       nread += read(fd,buf+nread,maxcnt-nread);
  2178.     if(nread == -1 && errno == EWOULDBLOCK)
  2179.       nread = 0;
  2180.     set_blocking(fd,True);
  2181.     return nread;
  2182.   }
  2183.  
  2184.   /* Most difficult - timeout read */
  2185.   /* If this is ever called on a disk file and 
  2186.      mincnt is greater then the filesize then
  2187.      system performance will suffer severely as 
  2188.      select always return true on disk files */
  2189.  
  2190.   /* Set initial timeout */
  2191.   timeout.tv_sec = time_out / 1000;
  2192.   timeout.tv_usec = 1000 * (time_out % 1000);
  2193.  
  2194.   /* As most UNIXes don't modify the value of timeout
  2195.      when they return from select we need to get the timeofday (in usec)
  2196.      now, and also after the select returns so we know
  2197.      how much time has elapsed */
  2198.  
  2199.   if (exact)
  2200.     GetTimeOfDay( &tval1);
  2201.   nread = 0; /* Number of bytes we have read */
  2202.  
  2203.   for(;;) 
  2204.     {
  2205.       
  2206.       FD_ZERO(&fds);
  2207.       FD_SET(fd,&fds);
  2208.       
  2209.       selrtn = sys_select(&fds,&timeout);
  2210.       
  2211.       /* Check if error */
  2212.       if(selrtn == -1)
  2213.     {
  2214.       errno = EBADF;
  2215.       return -1;
  2216.     }
  2217.       
  2218.       /* Did we timeout ? */
  2219.       if (selrtn == 0 ) {
  2220.      if (nread < mincnt) return -1;
  2221.     break; /* Yes */
  2222.       }
  2223.       
  2224.       readret = read( fd, buf+nread, maxcnt-nread);
  2225.       if(readret <= 0)
  2226.     {
  2227.       /* force a particular error number for
  2228.          portability */
  2229.       errno = EBADF;
  2230.       return -1;
  2231.     }
  2232.  
  2233.       if (readret == 0)
  2234.     break;
  2235.       
  2236.       nread += readret;
  2237.       
  2238.       /* If we have read more than mincnt then return */
  2239.       if( nread >= mincnt )
  2240.     break;
  2241.  
  2242.       /* We need to do another select - but first reduce the
  2243.      time_out by the amount of time already elapsed - if
  2244.      this is less than zero then return */
  2245.       if (exact)
  2246.     {
  2247.       GetTimeOfDay( &tval2);
  2248.       (void)tval_sub( &tvaldiff, &tval2, &tval1);
  2249.       
  2250.       if( tval_sub( &timeout, &timeout, &tvaldiff) <= 0) 
  2251.         {
  2252.           /* We timed out */
  2253.           break;
  2254.         }
  2255.     }
  2256.       
  2257.       /* Save the time of day as we need to do the select 
  2258.      again (saves a system call)*/
  2259.       tval1 = tval2;
  2260.     }
  2261.  
  2262.   /* Return the number we got */
  2263.   return(nread);
  2264. }
  2265.  
  2266. /****************************************************************************
  2267. read data from the client. Maxtime is in milliseconds
  2268. ****************************************************************************/
  2269. int read_max_udp(int fd,char *buffer,int bufsize,int maxtime)
  2270. {
  2271.   fd_set fds;
  2272.   int selrtn;
  2273.   int nread;
  2274.   struct timeval timeout;
  2275.  
  2276.   FD_ZERO(&fds);
  2277.   FD_SET(fd,&fds);
  2278.  
  2279.   timeout.tv_sec = maxtime / 1000;
  2280.   timeout.tv_usec = (maxtime % 1000) * 1000;
  2281.  
  2282.   selrtn = sys_select(&fds,maxtime>0?&timeout:NULL);
  2283.  
  2284.   if (!FD_ISSET(fd,&fds))
  2285.     return 0;
  2286.  
  2287.   nread = read_udp_socket(fd, buffer, bufsize);
  2288.  
  2289.   /* return the number got */
  2290.   return(nread);
  2291. }
  2292.  
  2293. /*******************************************************************
  2294. find the difference in milliseconds between two struct timeval
  2295. values
  2296. ********************************************************************/
  2297. int TvalDiff(struct timeval *tvalold,struct timeval *tvalnew)
  2298. {
  2299.   return((tvalnew->tv_sec - tvalold->tv_sec)*1000 + 
  2300.      ((int)tvalnew->tv_usec - (int)tvalold->tv_usec)/1000);     
  2301. }
  2302.  
  2303. /****************************************************************************
  2304. send a keepalive packet (rfc1002)
  2305. ****************************************************************************/
  2306. BOOL send_keepalive(int client)
  2307. {
  2308.   unsigned char buf[4];
  2309.  
  2310.   buf[0] = 0x85;
  2311.   buf[1] = buf[2] = buf[3] = 0;
  2312.  
  2313.   return(write_data(client,(char *)buf,4) == 4);
  2314. }
  2315.  
  2316.  
  2317.  
  2318. /****************************************************************************
  2319.   read data from the client, reading exactly N bytes. 
  2320. ****************************************************************************/
  2321. int read_data(int fd,char *buffer,int N)
  2322. {
  2323.   int  ret;
  2324.   int total=0;  
  2325.  
  2326.   while (total < N)
  2327.     {
  2328.       ret = read(fd,buffer + total,N - total);
  2329.  
  2330.       /* this is for portability */
  2331.       if (ret < 0)
  2332.     errno = EBADF;
  2333.  
  2334.       if (ret <= 0)
  2335.     return total;
  2336.       total += ret;
  2337.     }
  2338.   return total;
  2339. }
  2340.  
  2341.  
  2342. /****************************************************************************
  2343.   write data to a fd 
  2344. ****************************************************************************/
  2345. int write_data(int fd,char *buffer,int N)
  2346. {
  2347.   int total=0;
  2348.   int ret;
  2349.  
  2350.   while (total < N)
  2351.     {
  2352.       ret = write(fd,buffer + total,N - total);
  2353.  
  2354.       if (ret <= 0)
  2355.     return total;
  2356.  
  2357.       total += ret;
  2358.     }
  2359.   return total;
  2360. }
  2361.  
  2362.  
  2363. /* variables used by the read prediction module */
  2364. int rp_fd = -1;
  2365. int rp_offset = 0;
  2366. int rp_length = 0;
  2367. int rp_alloced = 0;
  2368. int rp_predict_fd = -1;
  2369. int rp_predict_offset = 0;
  2370. int rp_predict_length = 0;
  2371. int rp_timeout = 5;
  2372. time_t rp_time = 0;
  2373. char *rp_buffer = NULL;
  2374. BOOL predict_skip=False;
  2375. time_t smb_last_time=(time_t)0;
  2376.  
  2377. /****************************************************************************
  2378. handle read prediction on a file
  2379. ****************************************************************************/
  2380. int read_predict(int fd,int offset,char *buf,char **ptr,int num)
  2381. {
  2382.   int ret = 0;
  2383.   int possible = rp_length - (offset - rp_offset);
  2384.  
  2385.   possible = MIN(possible,num);
  2386.  
  2387.   /* give data if possible */
  2388.   if (fd == rp_fd && 
  2389.       offset >= rp_offset && 
  2390.       possible>0 &&
  2391.       smb_last_time-rp_time < rp_timeout)
  2392.     {
  2393.       ret = possible;
  2394.       if (buf)
  2395.     memcpy(buf,rp_buffer + (offset-rp_offset),possible);
  2396.       else
  2397.     *ptr = rp_buffer + (offset-rp_offset);
  2398.       DEBUG(5,("read-prediction gave %d bytes of %d\n",ret,num));
  2399.     }
  2400.  
  2401.   if (ret == num) {
  2402.     predict_skip = True;
  2403.   } else {
  2404.     predict_skip = False;
  2405.  
  2406.     /* prepare the next prediction */
  2407.     rp_predict_fd = fd;
  2408.     rp_predict_offset = offset + num;
  2409.     rp_predict_length = num;
  2410.   }
  2411.  
  2412.   if (ret < 0) ret = 0;
  2413.  
  2414.   return(ret);
  2415. }
  2416.  
  2417. /****************************************************************************
  2418. pre-read some data
  2419. ****************************************************************************/
  2420. void do_read_prediction()
  2421. {
  2422.   if (predict_skip) return;
  2423.  
  2424.   if (rp_predict_fd == -1) 
  2425.     return;
  2426.  
  2427.   rp_fd = rp_predict_fd;
  2428.   rp_offset = rp_predict_offset;
  2429.   rp_length = 0;
  2430.  
  2431.   rp_predict_fd = -1;
  2432.  
  2433.   rp_predict_length = MIN(rp_predict_length,2*ReadSize);
  2434.   rp_predict_length = MAX(rp_predict_length,1024);
  2435.   rp_offset = (rp_offset/1024)*1024;
  2436.   rp_predict_length = (rp_predict_length/1024)*1024;
  2437.  
  2438.   if (rp_predict_length > rp_alloced)
  2439.     {
  2440.       rp_buffer = Realloc(rp_buffer,rp_predict_length);
  2441.       rp_alloced = rp_predict_length;
  2442.       if (!rp_buffer)
  2443.     {
  2444.       DEBUG(0,("can't allocate read-prediction buffer\n"));
  2445.       rp_predict_fd = -1;
  2446.       rp_fd = -1;
  2447.       rp_alloced = 0;
  2448.       return;
  2449.     }
  2450.     }
  2451.  
  2452.   if (lseek(rp_fd,rp_offset,SEEK_SET) != rp_offset) {
  2453.     rp_fd = -1;
  2454.     rp_predict_fd = -1;
  2455.     return;
  2456.   }
  2457.  
  2458.   rp_length = read(rp_fd,rp_buffer,rp_predict_length);
  2459.   rp_time = time(NULL);
  2460.   if (rp_length < 0)
  2461.     rp_length = 0;
  2462. }
  2463.  
  2464. /****************************************************************************
  2465. invalidate read-prediction on a fd
  2466. ****************************************************************************/
  2467. void invalidate_read_prediction(int fd)
  2468. {
  2469.  if (rp_fd == fd) 
  2470.    rp_fd = -1;
  2471.  if (rp_predict_fd == fd)
  2472.    rp_predict_fd = -1;
  2473. }
  2474.  
  2475.  
  2476. /****************************************************************************
  2477. transfer some data between two fd's
  2478. ****************************************************************************/
  2479. int transfer_file(int infd,int outfd,int n,char *header,int headlen,int align)
  2480. {
  2481.   static char *buf=NULL;  
  2482.   char *buf1,*abuf;
  2483.   static int size = 0;
  2484.   int total = 0;
  2485.  
  2486.   DEBUG(4,("transfer_file %d  (head=%d) called\n",n,headlen));
  2487.  
  2488.   if ((size < ReadSize) && buf) {
  2489.     free(buf);
  2490.     buf = NULL;
  2491.   }
  2492.  
  2493.   size = MAX(ReadSize,1024);
  2494.  
  2495.   while (!buf && size>0) {
  2496.     buf = (char *)Realloc(buf,size+8);
  2497.     if (!buf) size /= 2;
  2498.   }
  2499.   if (!buf) {
  2500.     DEBUG(0,("Can't allocate transfer buffer!\n"));
  2501.     exit(1);
  2502.   }
  2503.  
  2504.   abuf = buf + (align%8);
  2505.  
  2506.   if (header)
  2507.     n += headlen;
  2508.  
  2509.   while (n > 0)
  2510.     {
  2511.       int s = MIN(n,size);
  2512.       int ret,ret2=0;
  2513.  
  2514.       ret = 0;
  2515.  
  2516.       if (header && (headlen >= MIN(s,1024))) {
  2517.     buf1 = header;
  2518.     s = headlen;
  2519.     ret = headlen;
  2520.     headlen = 0;
  2521.     header = NULL;
  2522.       } else {
  2523.     buf1 = abuf;
  2524.       }
  2525.  
  2526.       if (header && headlen > 0)
  2527.     {
  2528.       ret = MIN(headlen,size);
  2529.       memcpy(buf1,header,ret);
  2530.       headlen -= ret;
  2531.       header += ret;
  2532.       if (headlen <= 0) header = NULL;
  2533.     }
  2534.  
  2535.       if (s > ret)
  2536.     ret += read(infd,buf1+ret,s-ret);
  2537.  
  2538.       if (ret > 0)
  2539.     {
  2540.       ret2 = (outfd>=0?write_data(outfd,buf1,ret):ret);
  2541.       if (ret2 > 0) total += ret2;
  2542.       /* if we can't write then dump excess data */
  2543.       if (ret2 != ret)
  2544.         transfer_file(infd,-1,n-(ret+headlen),NULL,0,0);
  2545.     }
  2546.       if (ret <= 0 || ret2 != ret)
  2547.     return(total);
  2548.       n -= ret;
  2549.     }
  2550.   return(total);
  2551. }
  2552.  
  2553.  
  2554. /****************************************************************************
  2555. read 4 bytes of a smb packet and return the smb length of the packet
  2556. possibly store the result in the buffer
  2557. ****************************************************************************/
  2558. int read_smb_length(int fd,char *inbuf,int timeout)
  2559. {
  2560.   char *buffer;
  2561.   char buf[4];
  2562.   int len=0, msg_type;
  2563.   BOOL ok=False;
  2564.  
  2565.   if (inbuf)
  2566.     buffer = inbuf;
  2567.   else
  2568.     buffer = buf;
  2569.  
  2570.   while (!ok)
  2571.     {
  2572.       if (timeout > 0)
  2573.     ok = (read_with_timeout(fd,buffer,4,4,timeout,False) == 4);
  2574.       else    
  2575.     ok = (read_data(fd,buffer,4) == 4);
  2576.  
  2577.       if (!ok)
  2578.     {
  2579.       if (timeout>0)
  2580.         {
  2581.           DEBUG(10,("select timeout (%d)\n", timeout));
  2582.           return(-1);
  2583.         }
  2584.       else
  2585.         {
  2586.           DEBUG(6,("couldn't read from client\n"));
  2587.           exit(1);
  2588.         }
  2589.     }
  2590.  
  2591.       len = smb_len(buffer);
  2592.       msg_type = CVAL(buffer,0);
  2593.  
  2594.       if (msg_type == 0x85) 
  2595.     {
  2596.       DEBUG(5,( "Got keepalive packet\n"));
  2597.       ok = False;
  2598.     }
  2599.     }
  2600.  
  2601.   DEBUG(10,("got smb length of %d\n",len));
  2602.  
  2603.   return(len);
  2604. }
  2605.  
  2606.  
  2607.  
  2608. /****************************************************************************
  2609.   read an smb from a fd and return it's length
  2610. The timeout is in milli seconds
  2611. ****************************************************************************/
  2612. BOOL receive_smb(int fd,char *buffer,int timeout)
  2613. {
  2614.   int len;
  2615.   BOOL ok;
  2616.  
  2617.   bzero(buffer,smb_size + 100);
  2618.  
  2619.   len = read_smb_length(fd,buffer,timeout);
  2620.   if (len == -1)
  2621.     return(False);
  2622.  
  2623.   if (len > BUFFER_SIZE)
  2624.     {
  2625.       DEBUG(0,("Invalid packet length! (%d bytes)\n",len));
  2626.       if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
  2627.     exit(1);
  2628.     }
  2629.  
  2630.   ok = (read_data(fd,buffer+4,len) == len);
  2631.  
  2632.   if (!ok)
  2633.     {
  2634.       close_sockets();
  2635.       exit(1);
  2636.     }
  2637.  
  2638.   return(True);
  2639. }
  2640.  
  2641.  
  2642. /****************************************************************************
  2643.   send an smb to a fd 
  2644. ****************************************************************************/
  2645. BOOL send_smb(int fd,char *buffer)
  2646. {
  2647.   int len;
  2648.   int ret,nwritten=0;
  2649.   len = smb_len(buffer) + 4;
  2650.  
  2651.   while (nwritten < len)
  2652.     {
  2653.       ret = write_socket(fd,buffer+nwritten,len - nwritten);
  2654.       if (ret <= 0)
  2655.     {
  2656.       DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
  2657.           close_sockets();
  2658.       exit(1);
  2659.     }
  2660.       nwritten += ret;
  2661.     }
  2662.  
  2663.  
  2664.   return True;
  2665. }
  2666.  
  2667.  
  2668. /****************************************************************************
  2669. find a pointer to a netbios name
  2670. ****************************************************************************/
  2671. char *name_ptr(char *buf,int ofs)
  2672. {
  2673.   unsigned char c = *(unsigned char *)(buf+ofs);
  2674.  
  2675.   if ((c & 0xC0) == 0xC0)
  2676.     {
  2677.       uint16 l;
  2678.       char p[2];
  2679.       memcpy(p,buf+ofs,2);
  2680.       p[0] &= ~0xC0;
  2681.       l = RSVAL(p,0);
  2682.       DEBUG(5,("name ptr to pos %d from %d is %s\n",l,ofs,buf+l));
  2683.       return(buf + l);
  2684.     }
  2685.   else
  2686.     return(buf+ofs);
  2687. }  
  2688.  
  2689. /****************************************************************************
  2690. extract a netbios name from a buf
  2691. ****************************************************************************/
  2692. int name_extract(char *buf,int ofs,char *name)
  2693. {
  2694.   char *p = name_ptr(buf,ofs);
  2695.   int d = PTR_DIFF(p,buf+ofs);
  2696.   strcpy(name,"");
  2697.   if (d < -50 || d > 50) return(0);
  2698.   return(name_interpret(p,name));
  2699. }  
  2700.   
  2701.  
  2702. /****************************************************************************
  2703. return the total storage length of a mangled name
  2704. ****************************************************************************/
  2705. int name_len(char *s)
  2706. {
  2707.   char *s0=s;
  2708.   unsigned char c = *(unsigned char *)s;
  2709.   if ((c & 0xC0) == 0xC0)
  2710.     return(2);
  2711.   while (*s) s += (*s)+1;
  2712.   return(PTR_DIFF(s,s0)+1);
  2713. }
  2714.  
  2715. /****************************************************************************
  2716. send a single packet to a port on another machine
  2717. ****************************************************************************/
  2718. BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
  2719. {
  2720.   BOOL ret;
  2721.   int out_fd;
  2722.   struct sockaddr_in sock_out;
  2723.  
  2724.   if (passive)
  2725.     return(True);
  2726.  
  2727.   /* create a socket to write to */
  2728.   out_fd = socket(AF_INET, type, 0);
  2729.   if (out_fd == -1) 
  2730.     {
  2731.       DEBUG(0,("socket failed"));
  2732.       return False;
  2733.     }
  2734.  
  2735.   /* set the address and port */
  2736.   bzero((char *)&sock_out,sizeof(sock_out));
  2737.   putip((char *)&sock_out.sin_addr,(char *)&ip);
  2738.   sock_out.sin_port = htons( port );
  2739.   sock_out.sin_family = AF_INET;
  2740.   
  2741.   if (DEBUGLEVEL > 0)
  2742.     DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
  2743.          len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
  2744.     
  2745.   /* send it */
  2746.   ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
  2747.  
  2748.   if (!ret)
  2749.     DEBUG(0,("Packet send to %s(%d) failed ERRNO=%d\n",
  2750.          inet_ntoa(ip),port,errno));
  2751.  
  2752.   close(out_fd);
  2753.   return(ret);
  2754. }
  2755.  
  2756. /*******************************************************************
  2757. sleep for a specified number of milliseconds
  2758. ********************************************************************/
  2759. void msleep(int t)
  2760. {
  2761.   int tdiff=0;
  2762.   struct timeval tval,t1,t2;  
  2763.   fd_set fds;
  2764.  
  2765.   GetTimeOfDay(&t1);
  2766.   GetTimeOfDay(&t2);
  2767.   
  2768.   while (tdiff < t) {
  2769.     tval.tv_sec = (t-tdiff)/1000;
  2770.     tval.tv_usec = 1000*((t-tdiff)%1000);
  2771.  
  2772.     FD_ZERO(&fds);
  2773.     errno = 0;
  2774.     sys_select(&fds,&tval);
  2775.  
  2776.     GetTimeOfDay(&t2);
  2777.     tdiff = TvalDiff(&t1,&t2);
  2778.   }
  2779. }
  2780.  
  2781. /****************************************************************************
  2782. check if a string is part of a list
  2783. ****************************************************************************/
  2784. BOOL in_list(char *s,char *list,BOOL casesensitive)
  2785. {
  2786.   pstring tok;
  2787.   char *p=list;
  2788.  
  2789.   if (!list) return(False);
  2790.  
  2791.   while (next_token(&p,tok,LIST_SEP))
  2792.     {
  2793.       if (casesensitive) {
  2794.     if (strcmp(tok,s) == 0)
  2795.       return(True);
  2796.       } else {
  2797.     if (StrCaseCmp(tok,s) == 0)
  2798.       return(True);
  2799.       }
  2800.     }
  2801.   return(False);
  2802. }
  2803.  
  2804. /* this is used to prevent lots of mallocs of size 1 */
  2805. static char *null_string = NULL;
  2806.  
  2807. /****************************************************************************
  2808. set a string value, allocing the space for the string
  2809. ****************************************************************************/
  2810. BOOL string_init(char **dest,char *src)
  2811. {
  2812.   int l;
  2813.   if (!src)     
  2814.     src = "";
  2815.  
  2816.   l = strlen(src);
  2817.  
  2818.   if (l == 0)
  2819.     {
  2820.       if (!null_string)
  2821.     null_string = (char *)malloc(1);
  2822.  
  2823.       *null_string = 0;
  2824.       *dest = null_string;
  2825.     }
  2826.   else
  2827.     {
  2828.       *dest = (char *)malloc(l+1);
  2829.       strcpy(*dest,src);
  2830.     }
  2831.   return(True);
  2832. }
  2833.  
  2834. /****************************************************************************
  2835. free a string value
  2836. ****************************************************************************/
  2837. void string_free(char **s)
  2838. {
  2839.   if (!s || !(*s)) return;
  2840.   if (*s == null_string)
  2841.     *s = NULL;
  2842.   if (*s) free(*s);
  2843.   *s = NULL;
  2844. }
  2845.  
  2846. /****************************************************************************
  2847. set a string value, allocing the space for the string, and deallocating any 
  2848. existing space
  2849. ****************************************************************************/
  2850. BOOL string_set(char **dest,char *src)
  2851. {
  2852.   string_free(dest);
  2853.  
  2854.   return(string_init(dest,src));
  2855. }
  2856.  
  2857. /****************************************************************************
  2858. substitute a string for a pattern in another string. Make sure there is 
  2859. enough room!
  2860.  
  2861. This routine looks for pattern in s and replaces it with 
  2862. insert. It may do multiple replacements.
  2863.  
  2864. return True if a substitution was done.
  2865. ****************************************************************************/
  2866. BOOL string_sub(char *s,char *pattern,char *insert)
  2867. {
  2868.   BOOL ret = False;
  2869.   char *p;
  2870.   int ls,lp,li;
  2871.  
  2872.   if (!insert || !pattern || !s) return(False);
  2873.  
  2874.   ls = strlen(s);
  2875.   lp = strlen(pattern);
  2876.   li = strlen(insert);
  2877.  
  2878.   if (!*pattern) return(False);
  2879.  
  2880.   while (lp <= ls && (p = strstr(s,pattern)))
  2881.     {
  2882.       ret = True;
  2883.       memmove(p+li,p+lp,ls + 1 - (PTR_DIFF(p,s) + lp));
  2884.       memcpy(p,insert,li);
  2885.       s = p + li;
  2886.       ls = strlen(s);
  2887.     }
  2888.   return(ret);
  2889. }
  2890.  
  2891.  
  2892.  
  2893. /*********************************************************
  2894. * Recursive routine that is called by mask_match.
  2895. * Does the actual matching.
  2896. *********************************************************/
  2897. BOOL do_match(char *str, char *regexp, int case_sig)
  2898. {
  2899.   char *p;
  2900.  
  2901.   for( p = regexp; *p && *str; ) {
  2902.     switch(*p) {
  2903.     case '?':
  2904.       str++; p++;
  2905.       break;
  2906.  
  2907.     case '*':
  2908.       /* Look for a character matching 
  2909.      the one after the '*' */
  2910.       p++;
  2911.       if(!*p)
  2912.     return True; /* Automatic match */
  2913.       while(*str) {
  2914.     while(*str && (case_sig ? (*p != *str) : (toupper(*p)!=toupper(*str))))
  2915.       str++;
  2916.     if(do_match(str,p,case_sig))
  2917.       return True;
  2918.     if(!*str)
  2919.       return False;
  2920.     else
  2921.       str++;
  2922.       }
  2923.       return False;
  2924.  
  2925.     default:
  2926.       if(case_sig) {
  2927.     if(*str != *p)
  2928.       return False;
  2929.       } else {
  2930.     if(toupper(*str) != toupper(*p))
  2931.       return False;
  2932.       }
  2933.       str++, p++;
  2934.       break;
  2935.     }
  2936.   }
  2937.   if(!*p && !*str)
  2938.     return True;
  2939.  
  2940.   if (!*p && str[0] == '.' && str[1] == 0)
  2941.     return(True);
  2942.   
  2943.   if (!*str && *p == '?')
  2944.     {
  2945.       while (*p == '?') p++;
  2946.       return(!*p);
  2947.     }
  2948.  
  2949.   if(!*str && (*p == '*' && p[1] == '\0'))
  2950.     return True;
  2951.   return False;
  2952. }
  2953.  
  2954.  
  2955. /*********************************************************
  2956. * Routine to match a given string with a regexp - uses
  2957. * simplified regexp that takes * and ? only. Case can be
  2958. * significant or not.
  2959. *********************************************************/
  2960. BOOL mask_match(char *str, char *regexp, int case_sig,BOOL trans2)
  2961. {
  2962.   char *p;
  2963.   pstring p1, p2;
  2964.   fstring ebase,eext,sbase,sext;
  2965.  
  2966.   BOOL matched;
  2967.  
  2968.   /* Make local copies of str and regexp */
  2969.   StrnCpy(p1,regexp,sizeof(pstring)-1);
  2970.   StrnCpy(p2,str,sizeof(pstring)-1);
  2971.  
  2972.   if (!strchr(p2,'.')) {
  2973.     strcat(p2,".");
  2974.   }
  2975.  
  2976. /*
  2977.   if (!strchr(p1,'.')) {
  2978.     strcat(p1,".");
  2979.   }
  2980. */
  2981.  
  2982. #if 0
  2983.   if (strchr(p1,'.'))
  2984.     {
  2985.       string_sub(p1,"*.*","*");
  2986.       string_sub(p1,".*","*");
  2987.     }
  2988. #endif
  2989.  
  2990.   /* Remove any *? and ** as they are meaningless */
  2991.   for(p = p1; *p; p++)
  2992.     while( *p == '*' && (p[1] == '?' ||p[1] == '*'))
  2993.       (void)strcpy( &p[1], &p[2]);
  2994.  
  2995.   if (strequal(p1,"*")) return(True);
  2996.  
  2997.   DEBUG(5,("mask_match str=<%s> regexp=<%s>, case_sig = %d\n", p2, p1, case_sig));
  2998.  
  2999.   if (trans2) {
  3000.     strcpy(ebase,p1);
  3001.     strcpy(sbase,p2);
  3002.   } else {
  3003.     if ((p=strrchr(p1,'.'))) {
  3004.       *p = 0;
  3005.       strcpy(ebase,p1);
  3006.       strcpy(eext,p+1);
  3007.     } else {
  3008.       strcpy(ebase,p1);
  3009.       eext[0] = 0;
  3010.     }
  3011.  
  3012.   if (!strequal(p2,".") && !strequal(p2,"..") && (p=strrchr(p2,'.'))) {
  3013.     *p = 0;
  3014.     strcpy(sbase,p2);
  3015.     strcpy(sext,p+1);
  3016.   } else {
  3017.     strcpy(sbase,p2);
  3018.     strcpy(sext,"");
  3019.   }
  3020.   }
  3021.  
  3022.   matched = do_match(sbase,ebase,case_sig) && 
  3023.     (trans2 || do_match(sext,eext,case_sig));
  3024.  
  3025.   DEBUG(5,("mask_match returning %d\n", matched));
  3026.  
  3027.   return matched;
  3028. }
  3029.  
  3030.  
  3031.  
  3032. /****************************************************************************
  3033. become a daemon, discarding the controlling terminal
  3034. ****************************************************************************/
  3035. void become_daemon(void)
  3036. {
  3037. #ifndef NO_FORK_DEBUG
  3038.   if (fork())
  3039.     exit(0);
  3040.  
  3041.   /* detach from the terminal */
  3042. #ifdef USE_SETSID
  3043.   setsid();
  3044. #else
  3045. #ifdef TIOCNOTTY
  3046.   {
  3047.     int i = open("/dev/tty", O_RDWR);
  3048.     if (i >= 0) 
  3049.       {
  3050.     ioctl(i, (int) TIOCNOTTY, (char *)0);      
  3051.     close(i);
  3052.       }
  3053.   }
  3054. #endif
  3055. #endif
  3056. #endif
  3057. }
  3058.  
  3059. /****************************************************************************
  3060. calculate the default netmask for an address
  3061. ****************************************************************************/
  3062. static void default_netmask(struct in_addr *inm, struct in_addr *iad)
  3063. {
  3064.   unsigned long ad = ntohl(iad->s_addr);
  3065.   unsigned long nm;
  3066.   /*
  3067.   ** Guess a netmask based on the class of the IP address given.
  3068.   */
  3069.   if ( (ad & 0x80000000) == 0 ) {
  3070.     /* class A address */
  3071.     nm = 0xFF000000;
  3072.   } else if ( (ad & 0xC0000000) == 0x80000000 ) {
  3073.     /* class B address */
  3074.     nm = 0xFFFF0000;
  3075.   } else if ( (ad & 0xE0000000) == 0xC0000000 ) {
  3076.     /* class C address */
  3077.     nm = 0xFFFFFF00;
  3078.   }  else {
  3079.     /* class D or E; netmask doesn't make much sense - guess 4 bits */
  3080.     nm =  0xFFFFFFF0;
  3081.   }
  3082.   inm->s_addr = htonl(nm);
  3083. }
  3084.  
  3085. /****************************************************************************
  3086.   get the broadcast address for our address 
  3087. (troyer@saifr00.ateng.az.honeywell.com)
  3088. ****************************************************************************/
  3089. void get_broadcast(struct in_addr *if_ipaddr,
  3090.                     struct in_addr *if_bcast,
  3091.                     struct in_addr *if_nmask)
  3092. {  
  3093.   BOOL found = False;
  3094. #ifndef NO_GET_BROADCAST
  3095.   int sock = -1;               /* AF_INET raw socket desc */
  3096.   char buff[1024];
  3097.   struct ifreq *ifr=NULL;
  3098.   int i;
  3099.  
  3100. #if defined(EVEREST)
  3101.   int n_interfaces;
  3102.   struct ifconf ifc;
  3103.   struct ifreq  *ifreqs;
  3104. #elif defined(USE_IFREQ)
  3105.   struct ifreq ifreq;
  3106.   struct strioctl strioctl;
  3107.   struct ifconf *ifc;
  3108. #else
  3109.   struct ifconf ifc;
  3110. #endif
  3111. #endif
  3112.  
  3113.   /* get a default netmask and broadcast */
  3114.   default_netmask(if_nmask, if_ipaddr);
  3115.  
  3116. #ifndef NO_GET_BROADCAST  
  3117.   /* Create a socket to the INET kernel. */
  3118. #if USE_SOCKRAW
  3119.   if ((sock = socket(AF_INET, SOCK_RAW, PF_INET )) < 0)
  3120. #else
  3121.     if ((sock = socket(AF_INET, SOCK_DGRAM, 0 )) < 0)
  3122. #endif
  3123.       {
  3124.         DEBUG(0,( "Unable to open socket to get broadcast address\n"));
  3125.         return;
  3126.       }
  3127.   
  3128.   /* Get a list of the configured interfaces */
  3129. #ifdef EVEREST
  3130.   /* This is part of SCO Openserver 5: The ioctls are no longer part
  3131.      if the lower level STREAMS interface glue. They are now real
  3132.      ioctl calls */
  3133.  
  3134.   if (ioctl(sock, SIOCGIFANUM, &n_interfaces) < 0) {
  3135.     DEBUG(0,( "SIOCGIFANUM: %s\n", strerror(errno)));
  3136.   } else {
  3137.     DEBUG(0,( "number of interfaces returned is: %d\n", n_interfaces));
  3138.  
  3139.     ifc.ifc_len = sizeof(struct ifreq) * n_interfaces;
  3140.     ifc.ifc_buf = (caddr_t) alloca(ifc.ifc_len);
  3141.  
  3142.     if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
  3143.       DEBUG(0, ( "SIOCGIFCONF: %s\n", strerror(errno)));
  3144.     else {
  3145.       ifr = ifc.ifc_req;
  3146.  
  3147.       for (i = 0; i < n_interfaces; ++i) {
  3148.     if (if_ipaddr->s_addr ==
  3149.         ((struct sockaddr_in *) &ifr[i].ifr_addr)->sin_addr.s_addr) {
  3150.       found = True;
  3151.       break;
  3152.     }
  3153.       }
  3154.     }
  3155.   }
  3156. #elif defined(USE_IFREQ)
  3157.   ifc = (struct ifconf *)buff;
  3158.   ifc->ifc_len = BUFSIZ - sizeof(struct ifconf);
  3159.   strioctl.ic_cmd = SIOCGIFCONF;
  3160.   strioctl.ic_dp  = (char *)ifc;
  3161.   strioctl.ic_len = sizeof(buff);
  3162.   if (ioctl(sock, I_STR, &strioctl) < 0) {
  3163.     DEBUG(0,( "I_STR/SIOCGIFCONF: %s\n", strerror(errno)));
  3164.   } else {
  3165.     ifr = (struct ifreq *)ifc->ifc_req;  
  3166.  
  3167.     /* Loop through interfaces, looking for given IP address */
  3168.     for (i = ifc->ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) {
  3169.       if (if_ipaddr->s_addr ==
  3170.       (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
  3171.     found = True;
  3172.     break;
  3173.       }
  3174.     }
  3175.   }
  3176. #elif defined(__FreeBSD__) || defined(NETBSD)
  3177.   ifc.ifc_len = sizeof(buff);
  3178.   ifc.ifc_buf = buff;
  3179.   if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
  3180.     DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno)));
  3181.   } else {
  3182.     ifr = ifc.ifc_req;
  3183.     /* Loop through interfaces, looking for given IP address */
  3184.     i = ifc.ifc_len;
  3185.     while (i > 0) {
  3186.       if (if_ipaddr->s_addr ==
  3187.       (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
  3188.     found = True;
  3189.     break;
  3190.       }
  3191.       i -= ifr->ifr_addr.sa_len + IFNAMSIZ;
  3192.       ifr = (struct ifreq*) ((char*) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ);
  3193.     }
  3194.   }
  3195. #else
  3196.   ifc.ifc_len = sizeof(buff);
  3197.   ifc.ifc_buf = buff;
  3198.   if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
  3199.     DEBUG(0,("SIOCGIFCONF: %s\n", strerror(errno)));
  3200.   } else {
  3201.     ifr = ifc.ifc_req;
  3202.   
  3203.     /* Loop through interfaces, looking for given IP address */
  3204.     for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++) {
  3205. #ifdef BSDI
  3206.       if (ioctl(sock, SIOCGIFADDR, ifr) < 0) break;
  3207. #endif
  3208.       if (if_ipaddr->s_addr ==
  3209.       (*(struct sockaddr_in *) &ifr->ifr_addr).sin_addr.s_addr) {
  3210.     found = True;
  3211.     break;
  3212.       }
  3213.     }
  3214.   }
  3215. #endif
  3216.   
  3217.   if (!found) {
  3218.     DEBUG(0,("No interface found for address %s\n", inet_ntoa(*if_ipaddr)));
  3219.   } else {
  3220.     /* Get the netmask address from the kernel */
  3221. #ifdef USE_IFREQ
  3222.     ifreq = *ifr;
  3223.   
  3224.     strioctl.ic_cmd = SIOCGIFNETMASK;
  3225.     strioctl.ic_dp  = (char *)&ifreq;
  3226.     strioctl.ic_len = sizeof(struct ifreq);
  3227.     if (ioctl(sock, I_STR, &strioctl) < 0)
  3228.       DEBUG(0,("Failed I_STR/SIOCGIFNETMASK: %s\n", strerror(errno)));
  3229.     else
  3230.       *if_nmask = ((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr;
  3231. #else
  3232.     if (ioctl(sock, SIOCGIFNETMASK, ifr) < 0)
  3233.       DEBUG(0,("SIOCGIFNETMASK failed\n"));
  3234.     else
  3235.       *if_nmask = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
  3236. #endif
  3237.  
  3238.     DEBUG(2,("Netmask for %s = %s\n", ifr->ifr_name,
  3239.          inet_ntoa(*if_nmask)));
  3240.   }
  3241.  
  3242.   /* Close up shop */
  3243.   (void) close(sock);
  3244.   
  3245. #endif
  3246.  
  3247.   /* sanity check on the netmask */
  3248.   {
  3249.     unsigned long nm = ntohl(if_nmask->s_addr);
  3250.     if ((nm >> 24) != 0xFF) {
  3251.       DEBUG(0,("Impossible netmask %s - using defaults\n",inet_ntoa(*if_nmask)));
  3252.       default_netmask(if_nmask, if_ipaddr);      
  3253.     }
  3254.   }
  3255.  
  3256.   /* derive the broadcast assuming a 1's broadcast, as this is what
  3257.      all MS operating systems do, we have to comply even if the unix
  3258.      box is setup differently */
  3259.   {
  3260.     unsigned long ad = ntohl(if_ipaddr->s_addr);
  3261.     unsigned long nm = ntohl(if_nmask->s_addr);
  3262.     unsigned long bc = (ad & nm) | (0xffffffff & ~nm);
  3263.     if_bcast->s_addr = htonl(bc);
  3264.   }
  3265.   
  3266.   DEBUG(2,("Derived broadcast address %s\n", inet_ntoa(*if_bcast)));
  3267. }  /* get_broadcast */
  3268.  
  3269.  
  3270. /****************************************************************************
  3271. put up a yes/no prompt
  3272. ****************************************************************************/
  3273. BOOL yesno(char *p)
  3274. {
  3275.   pstring ans;
  3276.   printf("%s",p);
  3277.  
  3278.   if (!fgets(ans,sizeof(ans)-1,stdin))
  3279.     return(False);
  3280.  
  3281.   if (*ans == 'y' || *ans == 'Y')
  3282.     return(True);
  3283.  
  3284.   return(False);
  3285. }
  3286.  
  3287. /****************************************************************************
  3288. read a line from a file with possible \ continuation chars. 
  3289. Blanks at the start or end of a line are stripped.
  3290. The string will be allocated if s2 is NULL
  3291. ****************************************************************************/
  3292. char *fgets_slash(char *s2,int maxlen,FILE *f)
  3293. {
  3294.   char *s=s2;
  3295.   int len = 0;
  3296.   int c;
  3297.   BOOL start_of_line = True;
  3298.  
  3299.   if (feof(f))
  3300.     return(NULL);
  3301.  
  3302.   if (!s2)
  3303.     {
  3304.       maxlen = MIN(maxlen,8);
  3305.       s = (char *)Realloc(s,maxlen);
  3306.     }
  3307.  
  3308.   if (!s || maxlen < 2) return(NULL);
  3309.  
  3310.   *s = 0;
  3311.  
  3312.   while (len < maxlen-1)
  3313.     {
  3314.       c = getc(f);
  3315.       switch (c)
  3316.     {
  3317.     case '\r':
  3318.       break;
  3319.     case '\n':
  3320.       while (len > 0 && s[len-1] == ' ')
  3321.         {
  3322.           s[--len] = 0;
  3323.         }
  3324.       if (len > 0 && s[len-1] == '\\')
  3325.         {
  3326.           s[--len] = 0;
  3327.           start_of_line = True;
  3328.           break;
  3329.         }
  3330.       return(s);
  3331.     case EOF:
  3332.       if (len <= 0 && !s2) 
  3333.         free(s);
  3334.       return(len>0?s:NULL);
  3335.     case ' ':
  3336.       if (start_of_line)
  3337.         break;
  3338.     default:
  3339.       start_of_line = False;
  3340.       s[len++] = c;
  3341.       s[len] = 0;
  3342.     }
  3343.       if (!s2 && len > maxlen-3)
  3344.     {
  3345.       maxlen *= 2;
  3346.       s = (char *)Realloc(s,maxlen);
  3347.       if (!s) return(NULL);
  3348.     }
  3349.     }
  3350.   return(s);
  3351. }
  3352.  
  3353.  
  3354.  
  3355. /****************************************************************************
  3356. set the length of a file from a filedescriptor.
  3357. Returns 0 on success, -1 on failure.
  3358. ****************************************************************************/
  3359. int set_filelen(int fd, long len)
  3360. {
  3361. /* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot
  3362.    extend a file with ftruncate. Provide alternate implementation
  3363.    for this */
  3364.  
  3365. #if FTRUNCATE_CAN_EXTEND
  3366.   return ftruncate(fd, len);
  3367. #else
  3368.   struct stat st;
  3369.   char c = 0;
  3370.   long currpos = lseek(fd, 0L, SEEK_CUR);
  3371.  
  3372.   if(currpos < 0)
  3373.     return -1;
  3374.   /* Do an fstat to see if the file is longer than
  3375.      the requested size (call ftruncate),
  3376.      or shorter, in which case seek to len - 1 and write 1
  3377.      byte of zero */
  3378.   if(fstat(fd, &st)<0)
  3379.     return -1;
  3380.  
  3381. #ifdef S_ISFIFO
  3382.   if (S_ISFIFO(st.st_mode)) return 0;
  3383. #endif
  3384.  
  3385.   if(st.st_size == len)
  3386.     return 0;
  3387.   if(st.st_size > len)
  3388.     return ftruncate(fd, len);
  3389.  
  3390.   if(lseek(fd, len-1, SEEK_SET) != len -1)
  3391.     return -1;
  3392.   if(write(fd, &c, 1)!=1)
  3393.     return -1;
  3394.   /* Seek to where we were */
  3395.   lseek(fd, currpos, SEEK_SET);
  3396.   return 0;
  3397. #endif
  3398. }
  3399.  
  3400.  
  3401. /****************************************************************************
  3402. return the byte checksum of some data
  3403. ****************************************************************************/
  3404. int byte_checksum(char *buf,int len)
  3405. {
  3406.   unsigned char *p = (unsigned char *)buf;
  3407.   int ret = 0;
  3408.   while (len--)
  3409.     ret += *p++;
  3410.   return(ret);
  3411. }
  3412.  
  3413.  
  3414.  
  3415. #ifdef HPUX
  3416. /****************************************************************************
  3417. this is a version of setbuffer() for those machines that only have setvbuf
  3418. ****************************************************************************/
  3419. void setbuffer(FILE *f,char *buf,int bufsize)
  3420. {
  3421.   setvbuf(f,buf,_IOFBF,bufsize);
  3422. }
  3423. #endif
  3424.  
  3425.  
  3426. /****************************************************************************
  3427. parse out a directory name from a path name. Assumes dos style filenames.
  3428. ****************************************************************************/
  3429. char *dirname_dos(char *path,char *buf)
  3430. {
  3431.   char *p = strrchr(path,'\\');
  3432.  
  3433.   if (!p)
  3434.     strcpy(buf,path);
  3435.   else
  3436.     {
  3437.       *p = 0;
  3438.       strcpy(buf,path);
  3439.       *p = '\\';
  3440.     }
  3441.  
  3442.   return(buf);
  3443. }
  3444.  
  3445.  
  3446. /****************************************************************************
  3447. parse out a filename from a path name. Assumes dos style filenames.
  3448. ****************************************************************************/
  3449. static char *filename_dos(char *path,char *buf)
  3450. {
  3451.   char *p = strrchr(path,'\\');
  3452.  
  3453.   if (!p)
  3454.     strcpy(buf,path);
  3455.   else
  3456.     strcpy(buf,p+1);
  3457.  
  3458.   return(buf);
  3459. }
  3460.  
  3461.  
  3462.  
  3463. /****************************************************************************
  3464. expand a pointer to be a particular size
  3465. ****************************************************************************/
  3466. void *Realloc(void *p,int size)
  3467. {
  3468.   void *ret=NULL;
  3469.   if (!p)
  3470.     ret = (void *)malloc(size);
  3471.   else
  3472.     ret = (void *)realloc(p,size);
  3473.  
  3474.   if (!ret)
  3475.     DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",size));
  3476.  
  3477.   return(ret);
  3478. }
  3479.  
  3480. /****************************************************************************
  3481. set the time on a file
  3482. ****************************************************************************/
  3483. BOOL set_filetime(char *fname,time_t mtime)
  3484. {  
  3485.   struct utimbuf times;
  3486.  
  3487.   if (null_mtime(mtime)) return(True);
  3488.  
  3489.   times.modtime = times.actime = mtime;
  3490.  
  3491.   if (sys_utime(fname,×)) {
  3492.     DEBUG(4,("set_filetime(%s) failed: %s\n",fname,strerror(errno)));
  3493.   }
  3494.     
  3495.   return(True);
  3496. }
  3497.  
  3498.  
  3499. #ifdef NOSTRDUP
  3500. /****************************************************************************
  3501. duplicate a string
  3502. ****************************************************************************/
  3503. char *strdup(char *s)
  3504. {
  3505.   char *ret = NULL;
  3506.   if (!s) return(NULL);
  3507.   ret = (char *)malloc(strlen(s)+1);
  3508.   if (!ret) return(NULL);
  3509.   strcpy(ret,s);
  3510.   return(ret);
  3511. }
  3512. #endif
  3513.  
  3514.  
  3515. /****************************************************************************
  3516.   Signal handler for SIGPIPE (write on a disconnected socket) 
  3517. ****************************************************************************/
  3518. void Abort(void )
  3519. {
  3520.   DEBUG(0,("Probably got SIGPIPE\nExiting\n"));
  3521.   exit(2);
  3522. }
  3523.  
  3524.  
  3525. #ifdef REPLACE_STRLEN
  3526. /****************************************************************************
  3527. a replacement strlen() that returns int for solaris
  3528. ****************************************************************************/
  3529. int Strlen(char *s)
  3530. {
  3531.   int ret=0;
  3532.   if (!s) return(0);
  3533.   while (*s++) ret++;
  3534.   return(ret);
  3535. }
  3536. #endif
  3537.  
  3538.  
  3539. /****************************************************************************
  3540. return a time at the start of the current month
  3541. ****************************************************************************/
  3542. time_t start_of_month(void)
  3543. {
  3544.   time_t t = time(NULL);
  3545.   struct tm *t2;
  3546.   
  3547.   t2 = gmtime(&t);
  3548.   
  3549.   t2->tm_mday = 1;
  3550.   t2->tm_hour = 0;
  3551.   t2->tm_min = 0;
  3552.   t2->tm_sec = 0;
  3553.   
  3554.   return(mktime(t2));
  3555. }
  3556.  
  3557.  
  3558. /*******************************************************************
  3559.   check for a sane unix date
  3560. ********************************************************************/
  3561. BOOL sane_unix_date(time_t unixdate)
  3562. {
  3563.   struct tm t,today;
  3564.   time_t t_today = time(NULL);
  3565.   
  3566.   t = *(LocalTime(&unixdate,LOCAL_TO_GMT));
  3567.   today = *(LocalTime(&t_today,LOCAL_TO_GMT));
  3568.   
  3569.   if (t.tm_year < 80)
  3570.     return(False);
  3571.   
  3572.   if (t.tm_year >  today.tm_year)
  3573.     return(False);
  3574.   
  3575.   if (t.tm_year == today.tm_year &&
  3576.       t.tm_mon > today.tm_mon)
  3577.     return(False);
  3578.   
  3579.   
  3580.   if (t.tm_year == today.tm_year &&
  3581.       t.tm_mon == today.tm_mon &&
  3582.       t.tm_mday > (today.tm_mday+1))
  3583.     return(False);
  3584.   
  3585.   return(True);
  3586. }
  3587.  
  3588.  
  3589.  
  3590. #ifdef NO_FTRUNCATE
  3591.  /*******************************************************************
  3592. ftruncate for operating systems that don't have it
  3593. ********************************************************************/
  3594. int ftruncate(int f,long l)
  3595. {
  3596.       struct  flock   fl;
  3597.  
  3598.       fl.l_whence = 0;
  3599.       fl.l_len = 0;
  3600.       fl.l_start = l;
  3601.       fl.l_type = F_WRLCK;
  3602.       return fcntl(f, F_FREESP, &fl);
  3603. }
  3604. #endif
  3605.  
  3606.  
  3607.  
  3608. /****************************************************************************
  3609. get my own name and IP
  3610. ****************************************************************************/
  3611. BOOL get_myname(char *myname,struct in_addr *ip)
  3612. {
  3613.   struct hostent *hp;
  3614.   pstring hostname;
  3615.  
  3616.   *hostname = 0;
  3617.  
  3618.   /* get my host name */
  3619.   if (gethostname(hostname, MAXHOSTNAMELEN) == -1) 
  3620.     {
  3621.       DEBUG(0,("gethostname failed\n"));
  3622.       return False;
  3623.     } 
  3624.  
  3625.   /* get host info */
  3626.   if ((hp = Get_Hostbyname(hostname)) == 0) 
  3627.     {
  3628.       DEBUG(0,( "Get_Hostbyname: Unknown host %s.\n",hostname));
  3629.       return False;
  3630.     }
  3631.  
  3632.   if (myname)
  3633.     {
  3634.       /* split off any parts after an initial . */
  3635.       char *p = strchr(hostname,'.');
  3636.       if (p) *p = 0;
  3637.  
  3638.       strcpy(myname,hostname);
  3639.     }
  3640.  
  3641.   if (ip)
  3642.     putip((char *)ip,(char *)hp->h_addr);
  3643.  
  3644.   return(True);
  3645. }
  3646.  
  3647.  
  3648. /****************************************************************************
  3649. true if two IP addresses are equal
  3650. ****************************************************************************/
  3651. BOOL ip_equal(struct in_addr ip1,struct in_addr ip2)
  3652. {
  3653.   unsigned long a1,a2;
  3654.   a1 = ntohl(ip1.s_addr);
  3655.   a2 = ntohl(ip2.s_addr);
  3656.   return(a1 == a2);
  3657. }
  3658.  
  3659.  
  3660. /****************************************************************************
  3661. open a socket of the specified type, port and address for incoming data
  3662. ****************************************************************************/
  3663. int open_socket_in(int type, int port, int dlevel)
  3664. {
  3665.   struct hostent *hp;
  3666.   struct sockaddr_in sock;
  3667.   pstring host_name;
  3668.   int res;
  3669.  
  3670.   /* get my host name */
  3671. #ifdef MAXHOSTNAMELEN
  3672.   if (gethostname(host_name, MAXHOSTNAMELEN) == -1) 
  3673. #else
  3674.   if (gethostname(host_name, sizeof(host_name)) == -1) 
  3675. #endif
  3676.     { DEBUG(0,("gethostname failed\n")); return -1; } 
  3677.  
  3678.   /* get host info */
  3679.   if ((hp = Get_Hostbyname(host_name)) == 0) 
  3680.     {
  3681.       DEBUG(0,( "Get_Hostbyname: Unknown host. %s\n",host_name));
  3682.       return -1;
  3683.     }
  3684.   
  3685.   bzero((char *)&sock,sizeof(sock));
  3686.   memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
  3687. #if defined(__FreeBSD__) || defined(NETBSD) /* XXX not the right ifdef */
  3688.   sock.sin_len = sizeof(sock);
  3689. #endif
  3690.   sock.sin_port = htons( port );
  3691.   sock.sin_family = hp->h_addrtype;
  3692.   sock.sin_addr.s_addr = INADDR_ANY;
  3693.   res = socket(hp->h_addrtype, type, 0);
  3694.   if (res == -1) 
  3695.     { DEBUG(0,("socket failed\n")); return -1; }
  3696.  
  3697.   {
  3698.     int one=1;
  3699.     setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
  3700.   }
  3701.  
  3702.   /* now we've got a socket - we need to bind it */
  3703.   if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) 
  3704.     { 
  3705.       if (port) {
  3706.     if (port == 139 || port == 137)
  3707.       DEBUG(dlevel,("bind failed on port %d (%s)\n",
  3708.             port,strerror(errno))); 
  3709.     close(res); 
  3710.  
  3711.     if (dlevel > 0 && port < 1000)
  3712.       port = 7999;
  3713.  
  3714.     if (port >= 1000 && port < 9000)
  3715.       return(open_socket_in(type,port+1,dlevel));
  3716.       }
  3717.  
  3718.       return(-1); 
  3719.     }
  3720.   DEBUG(3,("bind succeeded on port %d\n",port));
  3721.  
  3722.   return res;
  3723. }
  3724.  
  3725.  
  3726. /****************************************************************************
  3727.   create an outgoing socket
  3728.   **************************************************************************/
  3729. int open_socket_out(int type, struct in_addr *addr, int port )
  3730. {
  3731.   struct sockaddr_in sock_out;
  3732.   int res;
  3733.  
  3734.   /* create a socket to write to */
  3735.   res = socket(PF_INET, type, 0);
  3736.   if (res == -1) 
  3737.     { DEBUG(0,("socket error\n")); return -1; }
  3738.  
  3739.   if (type != SOCK_STREAM) return(res);
  3740.   
  3741.   bzero((char *)&sock_out,sizeof(sock_out));
  3742.   putip((char *)&sock_out.sin_addr,(char *)addr);
  3743.   
  3744.   sock_out.sin_port = htons( port );
  3745.   sock_out.sin_family = PF_INET;
  3746.  
  3747.   DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
  3748.   
  3749.   /* and connect it to the destination */
  3750.   if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))<0) {
  3751.     DEBUG(0,("connect error: %s\n",strerror(errno))); 
  3752.     close(res); 
  3753.     return(-1);
  3754.   }
  3755.  
  3756.   return res;
  3757. }
  3758.  
  3759.  
  3760. /****************************************************************************
  3761. interpret a protocol description string, with a default
  3762. ****************************************************************************/
  3763. int interpret_protocol(char *str,int def)
  3764. {
  3765.   if (strequal(str,"NT1"))
  3766.     return(PROTOCOL_NT1);
  3767.   if (strequal(str,"LANMAN2"))
  3768.     return(PROTOCOL_LANMAN2);
  3769.   if (strequal(str,"LANMAN1"))
  3770.     return(PROTOCOL_LANMAN1);
  3771.   if (strequal(str,"CORE"))
  3772.     return(PROTOCOL_CORE);
  3773.   if (strequal(str,"COREPLUS"))
  3774.     return(PROTOCOL_COREPLUS);
  3775.   if (strequal(str,"CORE+"))
  3776.     return(PROTOCOL_COREPLUS);
  3777.   
  3778.   DEBUG(0,("Unrecognised protocol level %s\n",str));
  3779.   
  3780.   return(def);
  3781. }
  3782.  
  3783. /****************************************************************************
  3784. interpret a security level
  3785. ****************************************************************************/
  3786. int interpret_security(char *str,int def)
  3787. {
  3788.   if (strequal(str,"SERVER"))
  3789.     return(SEC_SERVER);
  3790.   if (strequal(str,"USER"))
  3791.     return(SEC_USER);
  3792.   if (strequal(str,"SHARE"))
  3793.     return(SEC_SHARE);
  3794.   
  3795.   DEBUG(0,("Unrecognised security level %s\n",str));
  3796.   
  3797.   return(def);
  3798. }
  3799.  
  3800.  
  3801. /****************************************************************************
  3802. interpret an internet address or name into an IP address in 4 byte form
  3803. ****************************************************************************/
  3804. unsigned long interpret_addr(char *str)
  3805. {
  3806.   struct hostent *hp;
  3807.   unsigned long res;
  3808.  
  3809.   if (strcmp(str,"0.0.0.0") == 0) return(0);
  3810.   if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF);
  3811.  
  3812.   /* if it's in the form of an IP address then get the lib to interpret it */
  3813.   if (isdigit(str[0])) {
  3814.     res = inet_addr(str);
  3815.   } else {
  3816.     /* otherwise assume it's a network name of some sort and use Get_Hostbyname */
  3817.     if ((hp = Get_Hostbyname(str)) == 0) {
  3818.       DEBUG(3,("Get_Hostbyname: Unknown host. %s\n",str));
  3819.       return 0;
  3820.     }
  3821.     putip((char *)&res,(char *)hp->h_addr);
  3822.   }
  3823.  
  3824.   if (res == (unsigned long)-1) return(0);
  3825.  
  3826.   return(res);
  3827. }
  3828.  
  3829. /*******************************************************************
  3830.   a convenient addition to interpret_addr()
  3831.   ******************************************************************/
  3832. struct in_addr *interpret_addr2(char *str)
  3833. {
  3834.   static struct in_addr ret;
  3835.   unsigned long a = interpret_addr(str);
  3836.   putip((char *)&ret,(char *)&a);
  3837.   return(&ret);
  3838. }
  3839.  
  3840. /*******************************************************************
  3841.   check if an IP is the 0.0.0.0
  3842.   ******************************************************************/
  3843. BOOL zero_ip(struct in_addr ip)
  3844. {
  3845.   unsigned long a;
  3846.   putip((char *)&a,(char *)&ip);
  3847.   return(a == 0);
  3848. }
  3849.  
  3850. #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
  3851.  
  3852. /****************************************************************************
  3853. interpret an 8 byte "filetime" structure to a time_t
  3854. It's originally in "100ns units since jan 1st 1601"
  3855.  
  3856. It appears to be kludge-GMT (at least for file listings). This means
  3857. its the GMT you get by taking a localtime and adding the
  3858. serverzone. This is NOT the same as GMT in some cases. This routine
  3859. converts this to real GMT.
  3860. ****************************************************************************/
  3861. time_t interpret_long_date(char *p)
  3862. {
  3863.   double d;
  3864.   time_t ret;
  3865.   uint32 tlow,thigh;
  3866.   tlow = IVAL(p,0);
  3867.   thigh = IVAL(p,4);
  3868.  
  3869.   if (thigh == 0) return(0);
  3870.  
  3871.   d = ((double)thigh)*4.0*(double)(1<<30);
  3872.   d += (tlow&0xFFF00000);
  3873.   d *= 1.0e-7;
  3874.  
  3875.   /* now adjust by 369 years to make the secs since 1970 */
  3876.   d -= TIME_FIXUP_CONSTANT;
  3877.  
  3878.   if (d>=MAXINT)
  3879.     return(0);
  3880.  
  3881.   ret = (time_t)(d+0.5);
  3882.  
  3883.   /* this takes us from kludge-GMT to real GMT */
  3884.   ret += TimeDiff(ret) - serverzone;
  3885.  
  3886.   return(ret);
  3887. }
  3888.  
  3889.  
  3890. /****************************************************************************
  3891. put a 8 byte filetime from a time_t
  3892. This takes real GMT as input and converts to kludge-GMT
  3893. ****************************************************************************/
  3894. void put_long_date(char *p,time_t t)
  3895. {
  3896.   uint32 tlow,thigh;
  3897.   double d;
  3898.  
  3899.   if (t==0) {
  3900.     SIVAL(p,0,0); SIVAL(p,4,0);
  3901.     return;
  3902.   }
  3903.  
  3904.   /* this converts GMT to kludge-GMT */
  3905.   t -= TimeDiff(t) - serverzone; 
  3906.  
  3907.   d = (double) (t);
  3908.  
  3909.   d += TIME_FIXUP_CONSTANT;
  3910.  
  3911.   d *= 1.0e7;
  3912.  
  3913.   thigh = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
  3914.   tlow = (uint32)(d - ((double)thigh)*4.0*(double)(1<<30));
  3915.  
  3916.   SIVAL(p,0,tlow);
  3917.   SIVAL(p,4,thigh);
  3918. }
  3919.  
  3920. /*******************************************************************
  3921. sub strings with useful parameters
  3922. ********************************************************************/
  3923. void standard_sub_basic(char *s)
  3924. {
  3925.   if (!strchr(s,'%')) return;
  3926.  
  3927.   string_sub(s,"%R",remote_proto);
  3928.   string_sub(s,"%a",remote_arch);
  3929.   string_sub(s,"%m",remote_machine);
  3930.   string_sub(s,"%L",local_machine);
  3931.  
  3932.   if (!strchr(s,'%')) return;
  3933.  
  3934.   string_sub(s,"%v",VERSION);
  3935.   string_sub(s,"%h",myhostname);
  3936.   string_sub(s,"%U",sesssetup_user);
  3937.  
  3938.   if (!strchr(s,'%')) return;
  3939.  
  3940.   string_sub(s,"%I",Client_info.addr);
  3941.   string_sub(s,"%M",Client_info.name);
  3942.   string_sub(s,"%T",timestring());
  3943.  
  3944.   if (!strchr(s,'%')) return;
  3945.  
  3946.   {
  3947.     char pidstr[10];
  3948.     sprintf(pidstr,"%d",(int)getpid());
  3949.     string_sub(s,"%d",pidstr);
  3950.   }
  3951. }
  3952.  
  3953.  
  3954. /*******************************************************************
  3955. write a string in unicoode format
  3956. ********************************************************************/
  3957. void PutUniCode(char *dst,char *src)
  3958. {
  3959.   while (*src) {
  3960.     dst[0] = src[0];
  3961.     dst[1] = 0;    
  3962.     dst += 2;
  3963.     src++;
  3964.   }
  3965.   dst[0] = dst[1] = 0;
  3966. }
  3967.  
  3968.  
  3969. pstring smbrun_path = SMBRUN;
  3970.  
  3971. /****************************************************************************
  3972. run a command via system() using smbrun
  3973. ****************************************************************************/
  3974. int smbrun(char *cmd,char *outfile)
  3975. {
  3976.   int ret;
  3977.   pstring syscmd;  
  3978.  
  3979.   if (!file_exist(smbrun_path,NULL))
  3980.     {
  3981.       DEBUG(0,("SMBRUN ERROR: Can't find %s. Installation problem?\n",smbrun_path));
  3982.       return(1);
  3983.     }
  3984.  
  3985.   sprintf(syscmd,"%s \"(%s 2>&1) > %s\"",
  3986.       smbrun_path,cmd,
  3987.       outfile?outfile:"/dev/null");
  3988.  
  3989.   DEBUG(5,("smbrun - running %s ",syscmd));
  3990.   ret = system(syscmd);
  3991.   DEBUG(5,("gave %d\n",ret));
  3992.   return(ret);
  3993. }
  3994.  
  3995.  
  3996. /****************************************************************************
  3997. a wrapper for gethostbyname() that tries with all lower and all upper case 
  3998. if the initial name fails
  3999. ****************************************************************************/
  4000. struct hostent *Get_Hostbyname(char *name)
  4001. {
  4002.   char *name2 = strdup(name);
  4003.   struct hostent *ret;
  4004.  
  4005.   if (!name2)
  4006.     {
  4007.       DEBUG(0,("Memory allocation error in Get_Hostbyname! panic\n"));
  4008.       exit(0);
  4009.     }
  4010.  
  4011.   if (!isalnum(*name2))
  4012.     {
  4013.       free(name2);
  4014.       return(NULL);
  4015.     }
  4016.  
  4017.   ret = gethostbyname(name2);
  4018.   if (ret != NULL)
  4019.     {
  4020.       free(name2);
  4021.       return(ret);
  4022.     }
  4023.  
  4024.   /* try with all lowercase */
  4025.   strlower(name2);
  4026.   ret = gethostbyname(name2);
  4027.   if (ret != NULL)
  4028.     {
  4029.       free(name2);
  4030.       return(ret);
  4031.     }
  4032.  
  4033.   /* try with all uppercase */
  4034.   strupper(name2);
  4035.   ret = gethostbyname(name2);
  4036.   if (ret != NULL)
  4037.     {
  4038.       free(name2);
  4039.       return(ret);
  4040.     }
  4041.   
  4042.   /* nothing works :-( */
  4043.   free(name2);
  4044.   return(NULL);
  4045. }
  4046.  
  4047.  
  4048. /****************************************************************************
  4049. check if a process exists. Does this work on all unixes?
  4050. ****************************************************************************/
  4051. BOOL process_exists(int pid)
  4052. {
  4053. #ifdef LINUX
  4054.   fstring s;
  4055.   sprintf(s,"/proc/%d",pid);
  4056.   return(directory_exist(s,NULL));
  4057. #else
  4058.   {
  4059.     static BOOL tested=False;
  4060.     static BOOL ok=False;
  4061.     fstring s;
  4062.     if (!tested) {
  4063.       tested = True;
  4064.       sprintf(s,"/proc/%05d",getpid());
  4065.       ok = file_exist(s,NULL);
  4066.     }
  4067.     if (ok) {
  4068.       sprintf(s,"/proc/%05d",pid);
  4069.       return(file_exist(s,NULL));
  4070.     }
  4071.   }
  4072.  
  4073.   /* a best guess for non root access */
  4074.   if (geteuid() != 0) return(True);
  4075.  
  4076.   /* otherwise use kill */
  4077.   return(pid == getpid() || kill(pid,0) == 0);
  4078. #endif
  4079. }
  4080.  
  4081.  
  4082. /*******************************************************************
  4083. turn a uid into a user name
  4084. ********************************************************************/
  4085. char *uidtoname(int uid)
  4086. {
  4087.   static char name[20];
  4088.   struct passwd *pass = getpwuid(uid);
  4089.   if (pass) return(pass->pw_name);
  4090.   sprintf(name,"%d",uid);
  4091.   return(name);
  4092. }
  4093.  
  4094. /*******************************************************************
  4095. turn a gid into a group name
  4096. ********************************************************************/
  4097. char *gidtoname(int gid)
  4098. {
  4099.   static char name[20];
  4100.   struct group *grp = getgrgid(gid);
  4101.   if (grp) return(grp->gr_name);
  4102.   sprintf(name,"%d",gid);
  4103.   return(name);
  4104. }
  4105.  
  4106. /*******************************************************************
  4107. block sigs
  4108. ********************************************************************/
  4109. void BlockSignals(BOOL block)
  4110. {
  4111. #ifdef USE_SIGBLOCK
  4112.   int block_mask = (sigmask(SIGTERM)|sigmask(SIGQUIT)|sigmask(SIGSEGV)
  4113.             |sigmask(SIGCHLD)|sigmask(SIGQUIT)|sigmask(SIGBUS)|
  4114.             sigmask(SIGINT));
  4115.   if (block) 
  4116.     sigblock(block_mask);
  4117.   else
  4118.     sigunblock(block_mask);
  4119. #endif
  4120. }
  4121.  
  4122. #if AJT
  4123. /*******************************************************************
  4124. my own panic function - not suitable for general use
  4125. ********************************************************************/
  4126. void ajt_panic(void)
  4127. {
  4128.   pstring cmd = "/usr/bin/X11/xedit -display :0 /tmp/ERROR_FAULT &";
  4129.   smbrun(cmd,NULL);
  4130. }
  4131. #endif
  4132.  
  4133. #ifdef USE_DIRECT
  4134. #define DIRECT direct
  4135. #else
  4136. #define DIRECT dirent
  4137. #endif
  4138.  
  4139. /*******************************************************************
  4140. a readdir wrapper which just returns the file name
  4141. also return the inode number if requested
  4142. ********************************************************************/
  4143. char *readdirname(void *p)
  4144. {
  4145.   struct DIRECT *ptr;
  4146.   char *dname;
  4147.  
  4148.   if (!p) return(NULL);
  4149.   
  4150.   ptr = (struct DIRECT *)readdir(p);
  4151.   if (!ptr) return(NULL);
  4152.  
  4153.   dname = ptr->d_name;
  4154.  
  4155. #ifdef KANJI
  4156.   {
  4157.     static pstring buf;
  4158.     strcpy(buf, dname);
  4159.     unix_to_dos(buf, True);
  4160.     dname = buf;
  4161.   }
  4162. #endif
  4163.  
  4164. #ifdef NEXT2
  4165.   if (telldir(p) < 0) return(NULL);
  4166. #endif
  4167.  
  4168. #ifdef SUNOS5
  4169.   /* this handles a broken compiler setup, causing a mixture
  4170.    of BSD and SYSV headers and libraries */
  4171.   {
  4172.     static BOOL broken_readdir = False;
  4173.     if (!broken_readdir && !(*(dname)) && strequal("..",dname-2))
  4174.       {
  4175.     DEBUG(0,("Your readdir() is broken. You have somehow mixed SYSV and BSD headers and libraries\n"));
  4176.     broken_readdir = True;
  4177.       }
  4178.     if (broken_readdir)
  4179.       return(dname-2);
  4180.   }
  4181. #endif
  4182.  
  4183.   return(dname);
  4184. }
  4185.  
  4186.  
  4187.  
  4188. #if (defined(SecureWare) && defined(SCO))
  4189. /* This is needed due to needing the nap() function but we don't want
  4190.    to include the Xenix libraries since that will break other things...
  4191.    BTW: system call # 0x0c28 is the same as calling nap() */
  4192. long nap(long milliseconds) {
  4193.   return syscall(0x0c28, milliseconds);
  4194. }
  4195. #endif
  4196.  
  4197. #ifdef NO_INITGROUPS
  4198. #include <sys/types.h>
  4199. #include <limits.h>
  4200. #include <grp.h>
  4201.  
  4202. #ifndef NULL
  4203. #define NULL (void *)0
  4204. #endif
  4205.  
  4206. /****************************************************************************
  4207.  some systems don't have an initgroups call 
  4208. ****************************************************************************/
  4209. int initgroups(char *name,gid_t id)
  4210. {
  4211. #ifdef NO_SETGROUPS
  4212.   /* yikes! no SETGROUPS or INITGROUPS? how can this work? */
  4213.   return(0);
  4214. #else
  4215.   gid_t  grouplst[NGROUPS_MAX];
  4216.   int    i,j;
  4217.   struct group *g;
  4218.   char   *gr;
  4219.  
  4220.   grouplst[0] = id;
  4221.   i = 1;
  4222.   while (i < NGROUPS_MAX && 
  4223.      ((g = (struct group *)getgrent()) != (struct group *)NULL)) 
  4224.     {
  4225.       if (g->gr_gid == id)
  4226.     continue;
  4227.       j = 0;
  4228.       gr = g->gr_mem[0];
  4229.       while (gr && (*gr != (char)NULL)) {
  4230.     if (strcmp(name,gr) == 0) {
  4231.       grouplst[i] = g->gr_gid;
  4232.       i++;
  4233.       gr = (char *)NULL;
  4234.       break;
  4235.     }
  4236.     gr = g->gr_mem[++j];
  4237.       }
  4238.     }
  4239.   endgrent();
  4240.   return(setgroups(i,grouplst));
  4241. #endif
  4242. }
  4243. #endif
  4244.  
  4245.  
  4246. #if WRAP_MALLOC
  4247.  
  4248. /* undo the wrapping temporarily */
  4249. #undef malloc
  4250. #undef realloc
  4251. #undef free
  4252.  
  4253. /****************************************************************************
  4254. wrapper for malloc() to catch memory errors
  4255. ****************************************************************************/
  4256. void *malloc_wrapped(int size,char *file,int line)
  4257. {
  4258. #ifdef xx_old_malloc
  4259.   void *res = xx_old_malloc(size);
  4260. #else
  4261.   void *res = malloc(size);
  4262. #endif
  4263.   DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
  4264.     file,line,
  4265.     size,(unsigned int)res));
  4266.   return(res);
  4267. }
  4268.  
  4269. /****************************************************************************
  4270. wrapper for realloc() to catch memory errors
  4271. ****************************************************************************/
  4272. void *realloc_wrapped(void *ptr,int size,char *file,int line)
  4273. {
  4274. #ifdef xx_old_realloc
  4275.   void *res = xx_old_realloc(ptr,size);
  4276. #else
  4277.   void *res = realloc(ptr,size);
  4278. #endif
  4279.   DEBUG(3,("Realloc\n"));
  4280.   DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
  4281.     file,line,
  4282.     (unsigned int)ptr));
  4283.   DEBUG(3,("Malloc called from %s(%d) with size=%d gave ptr=0x%X\n",
  4284.     file,line,
  4285.     size,(unsigned int)res));
  4286.   return(res);
  4287. }
  4288.  
  4289. /****************************************************************************
  4290. wrapper for free() to catch memory errors
  4291. ****************************************************************************/
  4292. void free_wrapped(void *ptr,char *file,int line)
  4293. {
  4294. #ifdef xx_old_free
  4295.   xx_old_free(ptr);
  4296. #else
  4297.   free(ptr);
  4298. #endif
  4299.   DEBUG(3,("free called from %s(%d) with ptr=0x%X\n",
  4300.     file,line,(unsigned int)ptr));
  4301.   return;
  4302. }
  4303.  
  4304. /* and re-do the define for spots lower in this file */
  4305. #define malloc(size) malloc_wrapped(size,__FILE__,__LINE__)
  4306. #define realloc(ptr,size) realloc_wrapped(ptr,size,__FILE__,__LINE__)
  4307. #define free(ptr) free_wrapped(ptr,__FILE__,__LINE__)
  4308.  
  4309. #endif
  4310.  
  4311. #ifdef REPLACE_STRSTR
  4312. /****************************************************************************
  4313. Mips version of strstr doesn't seem to work correctly.
  4314. There is a #define in includes.h to redirect calls to this function.
  4315. ****************************************************************************/
  4316. char *Strstr(char *s, char *p)
  4317. {
  4318.     int len = strlen(p);
  4319.  
  4320.     while ( *s != '\0' ) {
  4321.         if ( strncmp(s, p, len) == 0 )
  4322.         return s;
  4323.         s++;
  4324.     }
  4325.  
  4326.     return NULL;
  4327. }
  4328. #endif /* REPLACE_STRSTR */
  4329.  
  4330.  
  4331. #ifdef REPLACE_MKTIME
  4332. /*******************************************************************
  4333. a mktime() replacement for those who don't have it - contributed by 
  4334. C.A. Lademann <cal@zls.com>
  4335. ********************************************************************/
  4336. #define  MINUTE  60
  4337. #define  HOUR    60*MINUTE
  4338. #define  DAY             24*HOUR
  4339. #define  YEAR    365*DAY
  4340. time_t Mktime(struct tm      *t)
  4341. {
  4342.   struct tm       *u;
  4343.   time_t  epoch = 0;
  4344.   int             mon [] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  4345.   y, m, i;
  4346.  
  4347.   if(t->tm_year < 70)
  4348.     return((time_t)-1);
  4349.  
  4350.   epoch = (t->tm_year - 70) * YEAR + 
  4351.     (t->tm_year / 4 - 70 / 4 - t->tm_year / 100) * DAY;
  4352.  
  4353.   y = t->tm_year;
  4354.   m = 0;
  4355.  
  4356.   for(i = 0; i < t->tm_mon; i++) {
  4357.     epoch += mon [m] * DAY;
  4358.     if(m == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0))
  4359.       epoch += DAY;
  4360.     
  4361.     if(++m > 11) {
  4362.       m = 0;
  4363.       y++;
  4364.     }
  4365.   }
  4366.  
  4367.   epoch += (t->tm_mday - 1) * DAY;
  4368.   epoch += t->tm_hour * HOUR + t->tm_min * MINUTE + t->tm_sec;
  4369.   
  4370.   if((u = localtime(&epoch)) != NULL) {
  4371.     t->tm_sec = u->tm_sec;
  4372.     t->tm_min = u->tm_min;
  4373.     t->tm_hour = u->tm_hour;
  4374.     t->tm_mday = u->tm_mday;
  4375.     t->tm_mon = u->tm_mon;
  4376.     t->tm_year = u->tm_year;
  4377.     t->tm_wday = u->tm_wday;
  4378.     t->tm_yday = u->tm_yday;
  4379.     t->tm_isdst = u->tm_isdst;
  4380. #ifndef NO_TM_NAME
  4381.     memcpy(t->tm_name, u->tm_name, LTZNMAX);
  4382. #endif
  4383.   }
  4384.  
  4385.   return(epoch);
  4386. }
  4387. #endif /* REPLACE_MKTIME */
  4388.  
  4389.  
  4390.  
  4391. #ifdef REPLACE_RENAME
  4392. /* Rename a file. (from libiberty in GNU binutils)  */
  4393. int
  4394. rename (zfrom, zto)
  4395.      const char *zfrom;
  4396.      const char *zto;
  4397. {
  4398.   if (link (zfrom, zto) < 0)
  4399.     {
  4400.       if (errno != EEXIST)
  4401.     return -1;
  4402.       if (unlink (zto) < 0
  4403.       || link (zfrom, zto) < 0)
  4404.     return -1;
  4405.     }
  4406.   return unlink (zfrom);
  4407. }
  4408. #endif
  4409.  
  4410.  
  4411. #ifdef REPLACE_INNETGR
  4412. /*
  4413.  * Search for a match in a netgroup. This replaces it on broken systems.
  4414.  */
  4415. int InNetGr(group, host, user, dom)
  4416.         char *group, *host, *user, *dom;
  4417. {
  4418.   char *hst, *usr, *dm;
  4419.   
  4420.   setnetgrent(group);
  4421.   while (getnetgrent(&hst, &usr, &dm))
  4422.     if (((host == 0) || (hst == 0) || !strcmp(host, hst)) &&
  4423.     ((user == 0) || (usr == 0) || !strcmp(user, usr)) &&
  4424.     ((dom == 0) || (dm == 0) || !strcmp(dom, dm))) {
  4425.       endnetgrent();
  4426.       return (1);
  4427.     }
  4428.   endnetgrent();
  4429.   return (0);
  4430. }
  4431. #endif
  4432.  
  4433.  
  4434. #if WRAP_MEMCPY
  4435. #undef memcpy
  4436. /*******************************************************************
  4437. a wrapper around memcpy for diagnostic purposes
  4438. ********************************************************************/
  4439. void *memcpy_wrapped(void *d,void *s,int l,char *fname,int line)
  4440. {
  4441.   if (l>64 && (((int)d)%4) != (((int)s)%4))
  4442.     DEBUG(4,("Misaligned memcpy(0x%X,0x%X,%d) at %s(%d)\n",d,s,l,fname,line));
  4443. #ifdef xx_old_memcpy  
  4444.   return(xx_old_memcpy(d,s,l));
  4445. #else
  4446.   return(memcpy(d,s,l));
  4447. #endif
  4448. }
  4449. #define memcpy(d,s,l) memcpy_wrapped(d,s,l,__FILE__,__LINE__)
  4450. #endif
  4451.  
  4452.  
  4453.  
  4454.